From b6f92b91afcce23a8d835ce2d44a6fdd348a0619 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 12 Sep 2024 14:49:51 +0530 Subject: [PATCH] Add support for query annotation with name field --- .../openapi/service/mapper/Constants.java | 1 + .../mapper/parameter/QueryParameterMapper.java | 4 +++- .../mapper/utils/MapperCommonUtils.java | 18 ++++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/Constants.java b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/Constants.java index 05a0d675e..178339430 100644 --- a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/Constants.java +++ b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/Constants.java @@ -50,6 +50,7 @@ public final class Constants { public static final String ERROR_PAYLOAD = "ErrorPayload"; public static final String TREAT_NILABLE_AS_OPTIONAL = "treatNilableAsOptional"; public static final String HTTP_HEADER = "http:Header"; + public static final String HTTP_QUERY = "http:Query"; public static final String BYTE = "byte"; public static final String XML = "xml"; public static final String JSON = "json"; diff --git a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/parameter/QueryParameterMapper.java b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/parameter/QueryParameterMapper.java index 36a7b34b2..8e6aceaad 100644 --- a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/parameter/QueryParameterMapper.java +++ b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/parameter/QueryParameterMapper.java @@ -36,6 +36,7 @@ import java.util.Map; import java.util.Objects; +import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getQueryName; import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.unescapeIdentifier; /** @@ -62,7 +63,8 @@ public QueryParameterMapper(ParameterNode parameterNode, Map api Symbol parameterSymbol = additionalData.semanticModel().symbol(parameterNode).orElse(null); if (Objects.nonNull(parameterSymbol) && (parameterSymbol instanceof ParameterSymbol queryParameter)) { this.type = queryParameter.typeDescriptor(); - this.name = unescapeIdentifier(queryParameter.getName().get()); + String paramName = unescapeIdentifier(queryParameter.getName().get()); + this.name = getQueryName(parameterNode, paramName); this.isRequired = queryParameter.paramKind().equals(ParameterKind.REQUIRED); this.description = apiDocs.get(queryParameter.getName().get()); this.treatNilableAsOptional = treatNilableAsOptional; diff --git a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/utils/MapperCommonUtils.java b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/utils/MapperCommonUtils.java index ea5cf09a7..a62bbad46 100644 --- a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/utils/MapperCommonUtils.java +++ b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/utils/MapperCommonUtils.java @@ -99,6 +99,8 @@ import static io.ballerina.openapi.service.mapper.Constants.BALLERINA; import static io.ballerina.openapi.service.mapper.Constants.EMPTY; import static io.ballerina.openapi.service.mapper.Constants.HTTP; +import static io.ballerina.openapi.service.mapper.Constants.HTTP_HEADER; +import static io.ballerina.openapi.service.mapper.Constants.HTTP_QUERY; import static io.ballerina.openapi.service.mapper.Constants.HTTP_SERVICE_CONTRACT; import static io.ballerina.openapi.service.mapper.Constants.HYPHEN; import static io.ballerina.openapi.service.mapper.Constants.JSON_EXTENSION; @@ -439,6 +441,14 @@ public static String unescapeIdentifier(String parameterName) { } public static String getHeaderName(ParameterNode parameterNode, String defaultName) { + return getParamName(HTTP_HEADER, parameterNode, defaultName); + } + + public static String getQueryName(ParameterNode parameterNode, String defaultName) { + return getParamName(HTTP_QUERY, parameterNode, defaultName); + } + + public static String getParamName(String paramTypeName, ParameterNode parameterNode, String defaultName) { NodeList annotations = null; if (parameterNode instanceof RequiredParameterNode requiredParameterNode) { annotations = requiredParameterNode.annotations(); @@ -449,9 +459,9 @@ public static String getHeaderName(ParameterNode parameterNode, String defaultNa return defaultName; } for (AnnotationNode annotation : annotations) { - if (annotation.annotReference().toString().trim().equals("http:Header")) { - String valueExpression = getNameFromHeaderAnnotation(annotation); - if (valueExpression != null) { + if (annotation.annotReference().toString().trim().equals(paramTypeName)) { + String valueExpression = getNameFromParamAnnotation(annotation); + if (Objects.nonNull(valueExpression)) { return valueExpression; } } @@ -459,7 +469,7 @@ public static String getHeaderName(ParameterNode parameterNode, String defaultNa return defaultName; } - private static String getNameFromHeaderAnnotation(AnnotationNode annotation) { + private static String getNameFromParamAnnotation(AnnotationNode annotation) { Optional annotationRecord = annotation.annotValue(); if (annotationRecord.isEmpty()) { return null;