Skip to content

Commit

Permalink
Define the get type name interface
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jan 9, 2024
1 parent dcb2dac commit dae6198
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ public static OASResult generateOAS(OASGenerationMetaInfo oasGenerationMetaInfo)
// 03. Filter path and component sections in OAS.
// Generate openApi string for the mentioned service name.
convertServiceToOpenAPI(serviceDefinition, openapi, semanticModel, moduleMemberVisitor, diagnostics);
ConstraintMapper constraintMapper = new ConstraintMapper(openapi, moduleMemberVisitor, diagnostics);
ConstraintMapper constraintMapper = new ConstraintMapper(openapi, moduleMemberVisitor, semanticModel,
diagnostics);
constraintMapper.addMapping();
return new OASResult(openapi, diagnostics);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.ballerina.openapi.service.mapper.constraint;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.syntax.tree.AnnotationNode;
import io.ballerina.compiler.syntax.tree.ExpressionNode;
import io.ballerina.compiler.syntax.tree.MappingConstructorExpressionNode;
Expand Down Expand Up @@ -44,12 +45,14 @@ public class ConstraintMapper {

private final OpenAPI openAPI;
private final ModuleMemberVisitor moduleMemberVisitor;
private final SemanticModel semanticModel;
private final List<OpenAPIMapperDiagnostic> diagnostics;

public ConstraintMapper(OpenAPI openAPI, ModuleMemberVisitor moduleMemberVisitor,
public ConstraintMapper(OpenAPI openAPI, ModuleMemberVisitor moduleMemberVisitor, SemanticModel semanticModel,
List<OpenAPIMapperDiagnostic> diagnostics) {
this.openAPI = openAPI;
this.moduleMemberVisitor = moduleMemberVisitor;
this.semanticModel = semanticModel;
this.diagnostics = diagnostics;
}

Expand All @@ -60,7 +63,8 @@ public void addMapping() {
}
Map<String, Schema> schemas = components.getSchemas();
for (Map.Entry<String, Schema> schemaEntry : schemas.entrySet()) {
TypeDefinitionNode typeDefinitionNode = moduleMemberVisitor.getTypeDefinitionNode(schemaEntry.getKey());
TypeDefinitionNode typeDefinitionNode = moduleMemberVisitor.getTypeDefinitionNode(schemaEntry.getKey(),
semanticModel);
if (Objects.isNull(typeDefinitionNode)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@

package io.ballerina.openapi.service.mapper.model;

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.ListenerDeclarationNode;
import io.ballerina.compiler.syntax.tree.NodeVisitor;
import io.ballerina.compiler.syntax.tree.TypeDefinitionNode;
import io.ballerina.openapi.service.mapper.utils.MapperCommonUtils;

import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -50,9 +54,11 @@ public Set<ListenerDeclarationNode> getListenerDeclarationNodes() {
return listenerDeclarationNodes;
}

public TypeDefinitionNode getTypeDefinitionNode(String typeName) {
public TypeDefinitionNode getTypeDefinitionNode(String typeName, SemanticModel semanticModel) {
for (TypeDefinitionNode typeDefinitionNode : typeDefinitionNodes) {
if (MapperCommonUtils.unescapeIdentifier(typeDefinitionNode.typeName().text()).equals(typeName)) {
Optional<Symbol> symbol = semanticModel.symbol(typeDefinitionNode);
if (symbol.isPresent() && symbol.get() instanceof TypeSymbol typeSymbol &&
MapperCommonUtils.getTypeName(typeSymbol).equals(typeName)) {
return typeDefinitionNode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 @@ -139,7 +140,7 @@ public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol>
}
if (recordFieldSymbol.hasDefaultValue()) {
Object recordFieldDefaultValue = getRecordFieldDefaultValue(recordName, recordFieldName,
additionalData.moduleMemberVisitor());
additionalData.semanticModel(), additionalData.moduleMemberVisitor());
if (Objects.nonNull(recordFieldDefaultValue)) {
TypeMapper.setDefaultValue(recordFieldSchema, recordFieldDefaultValue);
} else {
Expand All @@ -154,9 +155,9 @@ public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol>
return properties;
}

public static Object getRecordFieldDefaultValue(String recordName, String fieldName,
public static Object getRecordFieldDefaultValue(String recordName, String fieldName, SemanticModel semanticModel,
ModuleMemberVisitor moduleMemberVisitor) {
TypeDefinitionNode recordDefNode = moduleMemberVisitor.getTypeDefinitionNode(recordName);
TypeDefinitionNode recordDefNode = moduleMemberVisitor.getTypeDefinitionNode(recordName, semanticModel);
if (Objects.isNull(recordDefNode) || !(recordDefNode.typeDescriptor() instanceof RecordTypeDescriptorNode)) {
return null;
}
Expand Down

0 comments on commit dae6198

Please sign in to comment.