Skip to content

Commit

Permalink
Merge pull request #871 from lnash94/beta_6_choreo
Browse files Browse the repository at this point in the history
[BallerinaToOAS][beta6] Fix issue with generated OAS for record field has array record with nullable
  • Loading branch information
hevayo authored Jan 21, 2022
2 parents 3a4e4a9 + b0f7708 commit f4334f7
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,11 @@ private void mapArrayToArraySchema(Map<String, Schema> schema, RecordFieldSymbol
Schema symbolProperty = ConverterCommonUtils.getOpenApiSchema(symbol.typeKind().getName());
// Handle record fields have union type array (ex: string[]? name)
if (symbol.typeKind() == TypeDescKind.UNION) {
symbolProperty = getSchemaForUnionType((UnionTypeSymbol) symbol, symbolProperty);
symbolProperty = getSchemaForUnionType((UnionTypeSymbol) symbol, symbolProperty, componentName, schema);
}
// Set the record model to the definition
if (symbol.typeKind().equals(TypeDescKind.TYPE_REFERENCE)) {
if (((TypeReferenceTypeSymbol) symbol).definition().kind() == SymbolKind.ENUM) {
TypeReferenceTypeSymbol typeRefEnum = (TypeReferenceTypeSymbol) symbol;
EnumSymbol enumSymbol = (EnumSymbol) typeRefEnum.definition();
symbolProperty = mapEnumValues(enumSymbol);
} else {
symbolProperty.set$ref(symbol.getName().orElseThrow().trim());
TypeReferenceTypeSymbol typeRecord = (TypeReferenceTypeSymbol) symbol;
if (!isSameRecord(componentName, typeRecord)) {
createComponentSchema(schema, typeRecord);
}
}
symbolProperty = getSchemaForTypeReferenceSymbol(symbol, symbolProperty, componentName, schema);
}
// Handle nested array type
if (arrayDimensions > 1) {
Expand All @@ -352,19 +342,46 @@ private void mapArrayToArraySchema(Map<String, Schema> schema, RecordFieldSymbol
* `string[]? name` here it takes union member types as array and nil,fix should do with array type and map to
* oneOf OAS.
*/
private Schema getSchemaForUnionType(UnionTypeSymbol symbol, Schema symbolProperty) {
private Schema getSchemaForUnionType(UnionTypeSymbol symbol, Schema symbolProperty, String componentName,
Map<String, Schema> schema) {
List<TypeSymbol> typeSymbols = symbol.userSpecifiedMemberTypes();
for (TypeSymbol typeSymbol: typeSymbols) {
if (typeSymbol.typeKind() == TypeDescKind.ARRAY) {
TypeSymbol arrayType = ((ArrayTypeSymbol) typeSymbol).memberTypeDescriptor();
symbolProperty = ConverterCommonUtils.getOpenApiSchema(arrayType.typeKind().getName());
// Set the record model to the definition
if (arrayType.typeKind().equals(TypeDescKind.TYPE_REFERENCE)) {
symbolProperty = getSchemaForTypeReferenceSymbol(arrayType, symbolProperty, componentName, schema);
} else {
symbolProperty = ConverterCommonUtils.getOpenApiSchema(arrayType.typeKind().getName());
}
} else if (typeSymbol.typeKind() != TypeDescKind.NIL) {
symbolProperty = ConverterCommonUtils.getOpenApiSchema(typeSymbol.typeKind().getName());
}
}
return symbolProperty;
}

/**
* This util function is to handle the type reference symbol is record type or enum type.
*
*/
private Schema getSchemaForTypeReferenceSymbol(TypeSymbol arrayType, Schema symbolProperty, String componentName,
Map<String, Schema> schema) {

if (((TypeReferenceTypeSymbol) arrayType).definition().kind() == SymbolKind.ENUM) {
TypeReferenceTypeSymbol typeRefEnum = (TypeReferenceTypeSymbol) arrayType;
EnumSymbol enumSymbol = (EnumSymbol) typeRefEnum.definition();
symbolProperty = mapEnumValues(enumSymbol);
} else {
symbolProperty.set$ref(arrayType.getName().orElseThrow().trim());
TypeReferenceTypeSymbol typeRecord = (TypeReferenceTypeSymbol) arrayType;
if (!isSameRecord(componentName, typeRecord)) {
createComponentSchema(schema, typeRecord);
}
}
return symbolProperty;
}

/**
* Handle nested array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,54 @@ paths:
$ref: '#/components/schemas/ResponseError03'
"202":
description: Accepted
/pet04:
post:
operationId: operation_post_/pet04
responses:
"200":
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseError04'
"202":
description: Accepted
/pet05:
post:
operationId: operation_post_/pet05
responses:
"200":
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseError05'
"202":
description: Accepted
/pet06:
post:
operationId: operation_post_/pet06
responses:
"200":
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseError06'
"202":
description: Accepted
/pet07:
post:
operationId: operation_post_/pet07
responses:
"200":
description: Ok
content:
application/json:
schema:
$ref: '#/components/schemas/ResponseError07'
"202":
description: Accepted
components:
schemas:
ResponseError:
Expand Down Expand Up @@ -85,3 +133,60 @@ components:
type: array
items:
$ref: '#/components/schemas/ResponseError03'
ResponseError04:
required:
- id
type: object
properties:
id:
type: integer
format: int32
nullable: true
resError:
type: array
items:
type: array
items:
$ref: '#/components/schemas/ResponseError04'
ResponseError05:
required:
- id
type: object
properties:
id:
type: integer
format: int32
nullable: true
resError:
nullable: true
oneOf:
- $ref: '#/components/schemas/ResponseError05'
ResponseError06:
required:
- id
type: object
properties:
id:
type: integer
format: int32
nullable: true
resError:
type: array
nullable: true
items:
$ref: '#/components/schemas/ResponseError06'
ResponseError07:
required:
- id
- resError
type: object
properties:
id:
type: integer
format: int32
nullable: true
resError:
type: array
nullable: true
items:
$ref: '#/components/schemas/ResponseError07'
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ type ResponseError03 record {|
ResponseError03[] resError?;
|};

type ResponseError04 record {|
int? id;
ResponseError04[][] resError?;
|};

type ResponseError05 record {|
int? id;
ResponseError05? resError?;
|};

type ResponseError06 record {|
int? id;
ResponseError06[]? resError?;
|};

type ResponseError07 record {|
int? id;
ResponseError07[]? resError;
|};

listener http:Listener ep0 = new (443, config = {host: "petstore.swagger.io"});

service /payloadV on ep0 {
Expand All @@ -30,4 +50,20 @@ service /payloadV on ep0 {
http:Accepted accept = {body: ()};
return accept;
}
resource function post pet04() returns ResponseError04|http:Accepted {
http:Accepted accept = {body: ()};
return accept;
}
resource function post pet05() returns ResponseError05|http:Accepted {
http:Accepted accept = {body: ()};
return accept;
}
resource function post pet06() returns ResponseError06|http:Accepted {
http:Accepted accept = {body: ()};
return accept;
}
resource function post pet07() returns ResponseError07|http:Accepted {
http:Accepted accept = {body: ()};
return accept;
}
}

0 comments on commit f4334f7

Please sign in to comment.