Skip to content

Commit

Permalink
Merge pull request #1782 from lnash94/master-bug-fixes
Browse files Browse the repository at this point in the history
[master] Fix OpenAPI spec generation issues
  • Loading branch information
lnash94 authored Nov 18, 2024
2 parents d1b1e18 + e5ebd89 commit 36a06e5
Show file tree
Hide file tree
Showing 22 changed files with 1,696 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package io.ballerina.openapi.service.mapper.parameter;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.api.symbols.TypeSymbol;
import io.ballerina.compiler.syntax.tree.DefaultableParameterNode;
import io.ballerina.compiler.syntax.tree.Node;
Expand All @@ -27,6 +28,9 @@

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static io.ballerina.openapi.service.mapper.utils.MapperCommonUtils.getConstantValues;

/**
* This {@link AbstractParameterMapper} class represents the abstract parameter mapper.
Expand Down Expand Up @@ -55,8 +59,13 @@ public void setParameter() throws ParameterMapperException {
parameterList.forEach(operationInventory::setParameter);
}

static Object getDefaultValue(DefaultableParameterNode parameterNode) {
static Object getDefaultValue(DefaultableParameterNode parameterNode, SemanticModel semanticModel) {
Node defaultValueExpression = parameterNode.expression();
Optional<Symbol> symbol = semanticModel.symbol(defaultValueExpression);
Optional<Object> constantValues = getConstantValues(symbol);
if (constantValues.isPresent()) {
return constantValues.get();
}
if (MapperCommonUtils.isNotSimpleValueLiteralKind(defaultValueExpression.kind())) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public HeaderParameterMapper(ParameterNode parameterNode, Map<String, String> ap
this.description = apiDocs.get(headerParameter.getName().get());
this.treatNilableAsOptional = treatNilableAsOptional;
if (parameterNode instanceof DefaultableParameterNode defaultableHeaderParam) {
this.defaultValue = AbstractParameterMapper.getDefaultValue(defaultableHeaderParam);
this.defaultValue = AbstractParameterMapper.getDefaultValue(defaultableHeaderParam,
additionalData.semanticModel());
}
this.typeMapper = typeMapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public QueryParameterMapper(ParameterNode parameterNode, Map<String, String> api
this.semanticModel = additionalData.semanticModel();
this.typeMapper = typeMapper;
if (parameterNode instanceof DefaultableParameterNode defaultableQueryParam) {
this.defaultValue = AbstractParameterMapper.getDefaultValue(defaultableQueryParam);
this.defaultValue = AbstractParameterMapper.getDefaultValue(defaultableQueryParam,
additionalData.semanticModel());
}
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.media.Schema;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -113,8 +114,11 @@ protected static void createComponentMapping(TypeReferenceTypeSymbol typeSymbol,
public Map<String, Schema> getSchemaForRecordFields(Map<String, RecordFieldSymbol> recordFieldMap,
Set<String> requiredFields, String recordName,
boolean treatNilableAsOptional) {
return RecordTypeMapper.mapRecordFields(recordFieldMap, components, requiredFields, recordName,
treatNilableAsOptional, false, componentMapperData);

RecordTypeMapper.RecordFieldMappingContext context = new RecordTypeMapper.RecordFieldMappingContext(
recordFieldMap, components, requiredFields, recordName, treatNilableAsOptional,
false, componentMapperData, new HashSet<>());
return RecordTypeMapper.mapRecordFields(context);
}

public TypeSymbol getReferredType(TypeSymbol typeSymbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,16 @@ public static Optional<ResourceFunction> getResourceFunction(Node function) {
return Optional.empty();
}

public static Optional<Object> getConstantValues(Optional<Symbol> symbol) {
if (symbol.isPresent() && symbol.get() instanceof ConstantSymbol constantSymbol) {
Object constValue = constantSymbol.constValue();
if (constValue instanceof ConstantValue value) {
return Optional.of(value.value());
}
}
return Optional.empty();
}

public static Node getTypeDescriptor(TypeDefinitionNode typeDefinitionNode) {
Node node = typeDefinitionNode.typeDescriptor();
if (node instanceof DistinctTypeDescriptorNode distinctTypeDescriptorNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public void testRecordWithNameFieldAnnotation() throws IOException {
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "record/record_field_with_name_annotation.yaml");
}

@Test(description = "Test for record has included record with same fields")
public void testIncludedRecordWithSameFields() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("record/included_record.bal");
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "record/included_record.yaml");
}

@AfterMethod
public void cleanUp() {
TestUtils.deleteDirectory(this.tempDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,22 @@ public void generateForSchemaHasObjectTypeOnly() throws IOException, BallerinaOp
serviceGenerationHandler.generateServiceFiles(oasServiceMetadata);
syntaxTree = TypeHandler.getInstance().generateTypeSyntaxTree();
}

@Test(description = "Generate record for schema has allOf types ")
public void generateTypeForTypeInclusions() throws IOException, BallerinaOpenApiException,
FormatterException {
Path definitionPath = RES_DIR.resolve("swagger/inclusion_types.yaml");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true, false);
TypeHandler.createInstance(openAPI, false);
ServiceGenerationHandler serviceGenerationHandler = new ServiceGenerationHandler();
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withNullable(false)
.withFilters(FILTER)
.build();
serviceGenerationHandler.generateServiceFiles(oasServiceMetadata);
syntaxTree = TypeHandler.getInstance().generateTypeSyntaxTree();
assertGeneratedSyntaxTreeContainsExpectedSyntaxTree("schema/ballerina/type_inclusion.bal",
syntaxTree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ paths:
- 1
- 2
- 3
- name: h8
in: header
schema:
type: string
default: Pod
- name: header32
in: header
required: true
schema:
type: array
items:
type: integer
format: int64
default:
- 1
- 2
- 3
- name: header31
in: header
schema:
type: string
default: Pod
responses:
"200":
description: Ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
/student12:
post:
operationId: postStudent12
parameters:
- name: status
in: query
schema:
type: string
default: Service
responses:
"201":
description: Created
content:
application/json:
schema:
type: object
"400":
description: BadRequest
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
/student13:
post:
operationId: postStudent13
parameters:
- name: kind
in: query
schema:
allOf:
- $ref: "#/components/schemas/ResourceKind"
default: Service
responses:
"201":
description: Created
content:
application/json:
schema:
type: object
"400":
description: BadRequest
content:
application/json:
schema:
$ref: "#/components/schemas/ErrorPayload"
components:
schemas:
ErrorPayload:
Expand All @@ -129,6 +174,11 @@ components:
type: string
method:
type: string
ResourceKind:
type: string
enum:
- Service
- Pod
Status:
type: string
enum:
Expand Down
Loading

0 comments on commit 36a06e5

Please sign in to comment.