diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java index 310ccc379..d1a2a97c7 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/Constants.java @@ -238,5 +238,8 @@ public String toString() { public static final String YML_EXTENSION = ".yml"; public static final String PLUS = "+"; public static final String UNDERSCORE = "_"; - + public static final String HTTP_202 = "202"; + public static final String HTTP_400 = "400"; + public static final String ACCEPTED = "Accepted"; + public static final String BAD_REQUEST = "BadRequest"; } diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java index 9308f66e3..8a278cbe3 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIRequestBodyMapper.java @@ -283,8 +283,8 @@ private String getMediaTypeForSyntaxKind(TypeDescriptorNode payloadNode) { return customMediaPrefix == null ? MediaType.APPLICATION_OCTET_STREAM : APPLICATION_PREFIX + customMediaPrefix + OCTECT_STREAM_POSTFIX; case SIMPLE_NAME_REFERENCE: - SimpleNameReferenceNode record = (SimpleNameReferenceNode) payloadNode; - TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(record)); + case QUALIFIED_NAME_REFERENCE: + TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(payloadNode)); if (typeSymbol instanceof TypeReferenceTypeSymbol) { typeSymbol = ((TypeReferenceTypeSymbol) typeSymbol).typeDescriptor(); } @@ -346,16 +346,21 @@ private void handleArrayTypePayload(Map schema, ArrayTypeDescrip TypeDescriptorNode typeDescriptorNode = arrayNode.memberTypeDesc(); // Nested array not allowed io.swagger.v3.oas.models.media.MediaType media = new io.swagger.v3.oas.models.media.MediaType(); - if (typeDescriptorNode.kind().equals(SyntaxKind.SIMPLE_NAME_REFERENCE)) { + if (typeDescriptorNode.kind().equals(SyntaxKind.SIMPLE_NAME_REFERENCE) || + typeDescriptorNode.kind().equals(SyntaxKind.QUALIFIED_NAME_REFERENCE)) { //handle record for components - SimpleNameReferenceNode referenceNode = (SimpleNameReferenceNode) typeDescriptorNode; - TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(referenceNode)); + TypeSymbol typeSymbol = getReferenceTypeSymbol(semanticModel.symbol(typeDescriptorNode)); OpenAPIComponentMapper componentMapper = new OpenAPIComponentMapper(components); componentMapper.createComponentSchema(schema, typeSymbol); diagnostics.addAll(componentMapper.getDiagnostics()); Schema itemSchema = new Schema(); - arraySchema.setItems(itemSchema.$ref(ConverterCommonUtils.unescapeIdentifier( - referenceNode.name().text().trim()))); + String referenceName; + if (typeDescriptorNode.kind().equals(SyntaxKind.SIMPLE_NAME_REFERENCE)) { + referenceName = ((SimpleNameReferenceNode) typeDescriptorNode).name().toString().trim(); + } else { + referenceName = ((QualifiedNameReferenceNode) typeDescriptorNode).identifier().text(); + } + arraySchema.setItems(itemSchema.$ref(ConverterCommonUtils.unescapeIdentifier(referenceName))); media.setSchema(arraySchema); } else if (typeDescriptorNode.kind() == SyntaxKind.BYTE_TYPE_DESC) { StringSchema byteSchema = new StringSchema(); diff --git a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java index b9c3f0242..c10a14045 100644 --- a/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java +++ b/openapi-bal-service/src/main/java/io/ballerina/openapi/converter/service/OpenAPIResponseMapper.java @@ -61,6 +61,7 @@ import io.ballerina.openapi.converter.utils.ConverterCommonUtils; import io.ballerina.tools.diagnostics.Location; import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.headers.Header; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.ComposedSchema; @@ -92,7 +93,9 @@ import static io.ballerina.compiler.syntax.tree.SyntaxKind.RECORD_FIELD; import static io.ballerina.compiler.syntax.tree.SyntaxKind.SIMPLE_NAME_REFERENCE; import static io.ballerina.compiler.syntax.tree.SyntaxKind.TYPE_REFERENCE; +import static io.ballerina.openapi.converter.Constants.ACCEPTED; import static io.ballerina.openapi.converter.Constants.APPLICATION_PREFIX; +import static io.ballerina.openapi.converter.Constants.BAD_REQUEST; import static io.ballerina.openapi.converter.Constants.BODY; import static io.ballerina.openapi.converter.Constants.BYTE; import static io.ballerina.openapi.converter.Constants.BYTE_ARRAY; @@ -105,7 +108,9 @@ import static io.ballerina.openapi.converter.Constants.HTTP_200_DESCRIPTION; import static io.ballerina.openapi.converter.Constants.HTTP_201; import static io.ballerina.openapi.converter.Constants.HTTP_201_DESCRIPTION; +import static io.ballerina.openapi.converter.Constants.HTTP_202; import static io.ballerina.openapi.converter.Constants.HTTP_204; +import static io.ballerina.openapi.converter.Constants.HTTP_400; import static io.ballerina.openapi.converter.Constants.HTTP_500; import static io.ballerina.openapi.converter.Constants.HTTP_500_DESCRIPTION; import static io.ballerina.openapi.converter.Constants.HTTP_CODES; @@ -197,8 +202,13 @@ public void getResourceOutput(FunctionDefinitionNode resource, OperationAdaptor } else { // When the return type is not mention in the resource function. ApiResponse apiResponse = new ApiResponse(); - apiResponse.description("Accepted"); - apiResponses.put("202", apiResponse); + apiResponse.description(ACCEPTED); + apiResponses.put(HTTP_202, apiResponse); + if (operation.getRequestBody() != null || operation.getParameters() != null) { + ApiResponse badRequestResponse = new ApiResponse(); + badRequestResponse.description(BAD_REQUEST); + apiResponses.put(HTTP_400, badRequestResponse); + } } operation.setResponses(apiResponses); } @@ -433,7 +443,17 @@ private Optional getAPIResponses(OperationAdaptor operationAdaptor io.swagger.v3.oas.models.media.MediaType mediaType = new io.swagger.v3.oas.models.media.MediaType(); String statusCode = httpMethod.equals(POST) ? HTTP_201 : HTTP_200; String description = httpMethod.equals(POST) ? HTTP_201_DESCRIPTION : HTTP_200_DESCRIPTION; - + if (typeNode.parent().kind() == SyntaxKind.OPTIONAL_TYPE_DESC) { + ApiResponse acceptedRequestResponse = new ApiResponse(); + acceptedRequestResponse.description(ACCEPTED); + apiResponses.put(HTTP_202, acceptedRequestResponse); + Operation operation = operationAdaptor.getOperation(); + if (operation.getRequestBody() != null || operation.getParameters() != null) { + ApiResponse badRequestResponse = new ApiResponse(); + badRequestResponse.description(BAD_REQUEST); + apiResponses.put(HTTP_400, badRequestResponse); + } + } String mediaTypeString; switch (typeNode.kind()) { case QUALIFIED_NAME_REFERENCE: @@ -790,11 +810,11 @@ private static void addResponse(ApiResponses apiResponses, Optional responses.forEach((key, value) -> { if (apiResponses.containsKey(key)) { ApiResponse res = apiResponses.get(key); - Content content = res.getContent(); - if (content == null) { - content = new Content(); - } if (value.getContent() != null) { + Content content = res.getContent(); + if (content == null) { + content = new Content(); + } String mediaType = value.getContent().keySet().iterator().next(); Schema newSchema = value.getContent().values().iterator().next().getSchema(); if (content.containsKey(mediaType)) { @@ -813,8 +833,8 @@ private static void addResponse(ApiResponses apiResponses, Optional +