Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jsondata:Name and http:Header mapping for record fields in the OpenAPI generation #1773

Merged
merged 12 commits into from
Sep 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public final class Constants {
public static final String HTTP_SERVICE_CONFIG = "http:ServiceConfig";
public static final String OPENAPI = "openapi";
public static final String HTTP = "http";
public static final String JSON_DATA = "data.jsondata";
public static final String NAME_CONFIG = "NameConfig";
public static final String NAME = "name";
public static final String BALLERINA = "ballerina";
public static final String EMPTY = "";
public static final String HTTP_SERVICE_CONTRACT_INFO = "ServiceContractInformation";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package io.ballerina.openapi.service.mapper.parameter;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.ParameterKind;
import io.ballerina.compiler.api.symbols.ParameterSymbol;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
Expand All @@ -41,7 +42,11 @@
import java.util.Set;
import java.util.stream.Collectors;

import static io.ballerina.openapi.service.mapper.Constants.HTTP;
import static io.ballerina.openapi.service.mapper.Constants.HTTP_HEADER_TYPE;
import static io.ballerina.openapi.service.mapper.Constants.NAME;
import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getHeaderName;
import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getNameFromAnnotation;
import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.unescapeIdentifier;

/**
Expand All @@ -59,12 +64,14 @@ public class HeaderParameterMapper extends AbstractParameterMapper {
private boolean treatNilableAsOptional = false;
private Object defaultValue = null;
private TypeMapper typeMapper = null;
private final SemanticModel semanticModel;

public HeaderParameterMapper(ParameterNode parameterNode, Map<String, String> apiDocs,
OperationInventory operationInventory, boolean treatNilableAsOptional,
AdditionalData additionalData, TypeMapper typeMapper) {
super(operationInventory);
Symbol parameterSymbol = additionalData.semanticModel().symbol(parameterNode).orElse(null);
this.semanticModel = additionalData.semanticModel();
if (Objects.nonNull(parameterSymbol) && (parameterSymbol instanceof ParameterSymbol headerParameter)) {
this.type = headerParameter.typeDescriptor();
String paramName = unescapeIdentifier(parameterSymbol.getName().get());
Expand Down Expand Up @@ -117,6 +124,7 @@ public List<Parameter> getRecordParameterSchema() {

Set<String> requiredHeaders = new HashSet<>();
HashMap<String, RecordFieldSymbol> headerMap = new HashMap<>(recordTypeInfo.typeSymbol().fieldDescriptors());
headerMap = updateHeaderMapWithName(headerMap);
Map<String, Schema> headerSchemaMap = typeMapper.getSchemaForRecordFields(headerMap, requiredHeaders,
recordTypeInfo.name(), treatNilableAsOptional);

Expand All @@ -126,6 +134,17 @@ public List<Parameter> getRecordParameterSchema() {
).collect(Collectors.toList());
}

private HashMap<String, RecordFieldSymbol> updateHeaderMapWithName(HashMap<String, RecordFieldSymbol> headerMap) {
HashMap<String, RecordFieldSymbol> updatedHeaderMap = new HashMap<>();
for (Map.Entry<String, RecordFieldSymbol> entry : headerMap.entrySet()) {
String defaultName = entry.getKey();
RecordFieldSymbol recordField = entry.getValue();
updatedHeaderMap.put(getNameFromAnnotation(HTTP, HTTP_HEADER_TYPE, NAME, semanticModel,
defaultName, recordField), recordField);
}
return updatedHeaderMap;
}

private Object getDefaultValueForHeaderField(String name) {
if (Objects.nonNull(defaultValue) && defaultValue instanceof Map defaultableValueMap) {
return defaultableValueMap.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package io.ballerina.openapi.service.mapper.type;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.IntersectionTypeSymbol;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
Expand Down Expand Up @@ -48,6 +49,10 @@
import java.util.Optional;
import java.util.Set;

import static io.ballerina.openapi.service.mapper.Constants.JSON_DATA;
import static io.ballerina.openapi.service.mapper.Constants.NAME_CONFIG;
import static io.ballerina.openapi.service.mapper.Constants.VALUE;
import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getNameFromAnnotation;
import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getRecordFieldTypeDescription;
import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getTypeName;

Expand Down Expand Up @@ -130,11 +135,13 @@ static List<Schema> mapIncludedRecords(RecordTypeSymbol typeSymbol, Components c
public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol> recordFieldMap,
Components components, Set<String> requiredFields,
String recordName, boolean treatNilableAsOptional,
boolean inferNameFromJsonData,
AdditionalData additionalData) {
Map<String, Schema> properties = new LinkedHashMap<>();
for (Map.Entry<String, RecordFieldSymbol> recordField : recordFieldMap.entrySet()) {
RecordFieldSymbol recordFieldSymbol = recordField.getValue();
String recordFieldName = MapperCommonUtils.unescapeIdentifier(recordField.getKey().trim());
String recordFieldName = getRecordFieldName(inferNameFromJsonData, recordField,
additionalData.semanticModel());
if (!recordFieldSymbol.isOptional() && !recordFieldSymbol.hasDefaultValue() &&
(!treatNilableAsOptional || !UnionTypeMapper.hasNilableType(recordFieldSymbol.typeDescriptor()))) {
requiredFields.add(recordFieldName);
Expand All @@ -161,6 +168,22 @@ public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol>
return properties;
}

public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol> recordFieldMap,
Components components, Set<String> requiredFields,
String recordName, boolean treatNilableAsOptional,
AdditionalData additionalData) {
return mapRecordFields(recordFieldMap, components, requiredFields, recordName, treatNilableAsOptional,
true, additionalData);
}

private static String getRecordFieldName(boolean inferNameFromJsonData,
Map.Entry<String, RecordFieldSymbol> recordFieldEntry,
SemanticModel semanticModel) {
String defaultName = MapperCommonUtils.unescapeIdentifier(recordFieldEntry.getKey().trim());
return inferNameFromJsonData ? getNameFromAnnotation(JSON_DATA, NAME_CONFIG, VALUE, semanticModel,
defaultName, recordFieldEntry.getValue()) : defaultName;
}

public static Optional<Object> getRecordFieldDefaultValue(String recordName, String fieldName,
ModuleMemberVisitor moduleMemberVisitor) {
Optional<TypeDefinitionNode> recordDefNodeOpt = moduleMemberVisitor.getTypeDefinitionNode(recordName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Map<String, Schema> getSchemaForRecordFields(Map<String, RecordFieldSymbo
Set<String> requiredFields, String recordName,
boolean treatNilableAsOptional) {
return RecordTypeMapper.mapRecordFields(recordFieldMap, components, requiredFields, recordName,
treatNilableAsOptional, componentMapperData);
treatNilableAsOptional, false, componentMapperData);
}

public TypeSymbol getReferredType(TypeSymbol typeSymbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.AnnotationAttachmentSymbol;
import io.ballerina.compiler.api.symbols.ConstantSymbol;
import io.ballerina.compiler.api.symbols.Documentable;
import io.ballerina.compiler.api.symbols.Documentation;
Expand All @@ -34,6 +35,7 @@
import io.ballerina.compiler.api.symbols.TypeReferenceTypeSymbol;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.compiler.api.symbols.UnionTypeSymbol;
import io.ballerina.compiler.api.values.ConstantValue;
import io.ballerina.compiler.syntax.tree.AnnotationNode;
import io.ballerina.compiler.syntax.tree.BasicLiteralNode;
import io.ballerina.compiler.syntax.tree.DefaultableParameterNode;
Expand Down Expand Up @@ -77,13 +79,15 @@
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.apache.commons.io.FilenameUtils;
import org.wso2.ballerinalang.compiler.tree.BLangConstantValue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
Expand Down Expand Up @@ -573,4 +577,63 @@ public static Node getTypeDescriptor(TypeDefinitionNode typeDefinitionNode) {
}
return node;
}

public static String getNameFromAnnotation(String packageName, String annotationName, String annotationFieldName,
SemanticModel semanticModel, String defaultName,
RecordFieldSymbol recordField) {
Optional<AnnotationAttachmentSymbol> annotationAttachment = getAnnotationAttachment(packageName,
annotationName, semanticModel, recordField.annotAttachments());
if (annotationAttachment.isPresent() && annotationAttachment.get().isConstAnnotation() &&
annotationAttachment.get().attachmentValue().isPresent()) {
Object value = annotationAttachment.get().attachmentValue().get().value();
Optional<String> name = getNameFromValue(annotationFieldName, value);
if (name.isPresent()) {
return name.get();
}
}
return defaultName;
}

private static Optional<AnnotationAttachmentSymbol> getAnnotationAttachment(String packageName,
String annotationName, SemanticModel semanticModel, List<AnnotationAttachmentSymbol> annotations) {
return annotations.stream()
.filter(annotAttachment -> isMatchingAnnotation(packageName, annotationName,
semanticModel, annotAttachment))
.findFirst();
}

private static boolean isMatchingAnnotation(String packageName, String annotationName, SemanticModel semanticModel,
AnnotationAttachmentSymbol annotAttachment) {
if (annotAttachment.typeDescriptor().typeDescriptor().isEmpty()) {
return false;
}
Optional<Symbol> exampleValueSymbol = semanticModel.types().getTypeByName(BALLERINA, packageName, EMPTY,
annotationName);
if (exampleValueSymbol.isEmpty() ||
!(exampleValueSymbol.get() instanceof TypeDefinitionSymbol serviceContractInfoType)) {
return false;
}
return annotAttachment.typeDescriptor().typeDescriptor().get()
.subtypeOf(serviceContractInfoType.typeDescriptor());
}

private static Optional<String> getNameFromValue(String annotationFieldName, Object value) {
if (value instanceof ConstantValue constantNameValue) {
value = constantNameValue.value();
} else if (value instanceof BLangConstantValue constantNameValue) {
value = constantNameValue.value;
}

if (!(value instanceof HashMap<?, ?> nameMap)) {
return Optional.empty();
}

Object name = nameMap.get(annotationFieldName);
if (Objects.isNull(name) || !(name instanceof ConstantValue constantName) ||
!(constantName.value() instanceof String nameFromAnnotation)) {
return Optional.empty();
}

return Optional.of(nameFromAnnotation);
}
}
30 changes: 30 additions & 0 deletions docs/ballerina-to-oas/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,36 @@ oneOf:
</tr>
</table>

> **Note:** If any field in the record type has a `jsondata:Name` annotation, the name specified in the annotation will be used as the schema name.
>
> Ballerina record type:
> ```ballerina
> public type Album record {|
> string artist;
> @jsondata:Name {value: "_id"}
> string id;
> string title;
> |};
> ```
>
> Generated schema:
> ```yml
> Album:
> required:
> - _id
> - artist
> - title
> type: object
> properties:
> _id:
> type: string
> title:
> type: string
> artist:
> type: string
> additionalProperties: false
> ```

### Ballerina constraints mapping to type schema

The Ballerina constraint package supports constraints on types. These constraints are mapped to the corresponding constraints in each type schema in the components section.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ stdlibJwtVersion=2.13.0
stdlibOAuth2Version=2.12.0

# Stdlib Level 05
stdlibHttpVersion=2.12.1-20240918-130700-9906dbe
stdlibHttpVersion=2.12.1-20240922-195800-82c5e76

# Stdlib Level 06
stdlibGrpcVersion=1.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ public Object[][] dataProviderForFunctionBody() {
{"diagnostic_files/header_parameter.yaml", "/pets", true, false, false,
"{string resourcePath=string`/pets`;" +
"map<string|string[]>" +
"httpHeaders=getMapForHeaders(headers);return self.clientEp->" +
"httpHeaders=http:getHeaderMap(headers);return self.clientEp->" +
"get(resourcePath,httpHeaders);}"},
{"diagnostic_files/head_operation.yaml", "/{filesystem}", true, false, true,
"{string resourcePath=string`/${getEncodedUri(filesystem)}`;" +
"resourcePath = resourcePath + check getPathForQueryParam(queries);" +
"map<string|string[]> httpHeaders = getMapForHeaders(headers);" +
"map<string|string[]> httpHeaders = http:getHeaderMap(headers);" +
"return self.clientEp-> head(resourcePath, httpHeaders);}"},
{"diagnostic_files/operation_delete.yaml", "/pets/{petId}", false, false, false,
"{string resourcePath = " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ public void testRecordWithDefaultValues() throws IOException {
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "record/record_with_default_values.yaml");
}

@Test(description = "Test for record fields with name field annotation")
public void testRecordWithNameFieldAnnotation() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("record/record_field_with_name_annotation.bal");
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "record/record_field_with_name_annotation.yaml");
}

@AfterMethod
public void cleanUp() {
TestUtils.deleteDirectory(this.tempDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ paths:
get:
operationId: getHeader
parameters:
- name: header14
in: header
required: true
schema:
type: boolean
- name: header12
in: header
required: true
Expand All @@ -35,11 +40,6 @@ paths:
required: true
schema:
type: string
- name: header14
in: header
required: true
schema:
type: boolean
- name: h0
in: header
schema:
Expand Down Expand Up @@ -89,18 +89,18 @@ paths:
- 1
- 2.3
- 4.56
- name: name
- name: city
in: header
required: true
schema:
type: string
default: John
- name: city
default: London
- name: name
in: header
required: true
schema:
type: string
default: London
default: John
- name: header23
in: header
required: true
Expand Down
Loading
Loading