Skip to content

Commit

Permalink
Merge pull request #1603 from lnash94/remove_unused_type_master
Browse files Browse the repository at this point in the history
[master] Remove unused types from generated types.bal in service generation
  • Loading branch information
lnash94 authored Feb 2, 2024
2 parents b6f4475 + cde9058 commit e62063c
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,10 @@ public void generateClientAndService(String definitionPath, String serviceName,
SyntaxTree schemaSyntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
String schemaContent = Formatter.format(schemaSyntaxTree).toSourceCode();

if (filter.getTags().size() > 0) {
// Remove unused records and enums when generating the client by the tags given.
schemaContent = GeneratorUtils.removeUnusedEntities(schemaSyntaxTree, clientContent, schemaContent,
serviceContent);
}
// Remove unused records and enums when generating the client and service.
schemaContent = GeneratorUtils.removeUnusedEntities(schemaSyntaxTree, clientContent, schemaContent,
serviceContent);

if (!schemaContent.isBlank()) {
sourceFiles.add(new GenSrcFile(GenSrcFile.GenFileType.MODEL_SRC, srcPackage, TYPE_FILE_NAME,
(licenseHeader.isBlank() ? DEFAULT_FILE_HEADER : licenseHeader) + schemaContent));
Expand Down Expand Up @@ -439,8 +438,10 @@ public List<GenSrcFile> generateBallerinaService(Path openAPI, String serviceNam
ballerinaServiceGenerator.getTypeInclusionRecords());
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(
openAPIDef, nullable, preGeneratedTypeDefNodes);
String schemaContent = Formatter.format(
ballerinaSchemaGenerator.generateSyntaxTree()).toSourceCode();
SyntaxTree schemaSyntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
String schemaContent = Formatter.format(schemaSyntaxTree).toString();
schemaContent = GeneratorUtils.removeUnusedEntities(schemaSyntaxTree, mainContent, schemaContent,
null);
if (!schemaContent.isBlank() && !generateWithoutDataBinding) {
sourceFiles.add(new GenSrcFile(GenSrcFile.GenFileType.GEN_SRC, srcPackage, TYPE_FILE_NAME,
(licenseHeader.isBlank() ? DEFAULT_FILE_HEADER : licenseHeader) + schemaContent));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ public type ProxyConfig record {|

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ public type ProxyConfig record {|

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ public type ProxyConfig record {|

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
Expand Down
10 changes: 0 additions & 10 deletions openapi-cli/src/test/resources/expected_gen/petstore_schema.bal
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ public type ProxyConfig record {|

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public type ProxyConfig record {|
string password = "";
|};

public type PetArr Pet[];

public type Pet record {
int petId;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ public type ProxyConfig record {|

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,3 @@ public type OkAnydata record {|
*http:Ok;
anydata body;
|};

public type Pets Pet[];

public type Error record {
int code;
string message;
};

public type Dog record {
*Pet;
boolean bark?;
};

public type Pet record {
int id;
string name;
string tag?;
string 'type?;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public type Coupon string;
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
package io.ballerina.openapi.core;

import io.ballerina.compiler.api.SemanticModel;
import io.ballerina.compiler.api.symbols.RecordFieldSymbol;
import io.ballerina.compiler.api.symbols.RecordTypeSymbol;
import io.ballerina.compiler.api.symbols.Symbol;
import io.ballerina.compiler.api.symbols.SymbolKind;
import io.ballerina.compiler.api.symbols.TypeDefinitionSymbol;
import io.ballerina.compiler.api.symbols.TypeDescKind;
import io.ballerina.compiler.syntax.tree.AbstractNodeFactory;
import io.ballerina.compiler.syntax.tree.AnnotationNode;
import io.ballerina.compiler.syntax.tree.BuiltinSimpleNameReferenceNode;
Expand Down Expand Up @@ -95,6 +99,7 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -890,7 +895,7 @@ public static String removeUnusedEntities(SyntaxTree schemaSyntaxTree, String cl
tempSourceFiles.put(CLIENT_FILE_NAME, clientContent);
tempSourceFiles.put(TYPE_FILE_NAME, schemaContent);
if (serviceContent != null) {
tempSourceFiles.put(SERVICE_FILE_NAME, schemaContent);
tempSourceFiles.put(SERVICE_FILE_NAME, serviceContent);
}
List<String> unusedTypeDefinitionNameList = getUnusedTypeDefinitionNameList(tempSourceFiles);
while (unusedTypeDefinitionNameList.size() > 0) {
Expand Down Expand Up @@ -998,15 +1003,23 @@ private static List<String> getUnusedTypeDefinitionNameList(Map<String, String>
List<String> unusedTypeDefinitionNameList = new ArrayList<>();
Path tmpDir = Files.createTempDirectory(".openapi-tmp" + System.nanoTime());
writeFilesTemp(srcFiles, tmpDir);
if (Files.exists(tmpDir.resolve(CLIENT_FILE_NAME)) && Files.exists(tmpDir.resolve(TYPE_FILE_NAME)) &&
if ((Files.exists(tmpDir.resolve(CLIENT_FILE_NAME)) || Files.exists(tmpDir.resolve(SERVICE_FILE_NAME)))
&& Files.exists(tmpDir.resolve(TYPE_FILE_NAME)) &&
Files.exists(tmpDir.resolve(BALLERINA_TOML))) {
SemanticModel semanticModel = getSemanticModel(tmpDir.resolve(CLIENT_FILE_NAME));
SemanticModel semanticModel = Files.exists(tmpDir.resolve(CLIENT_FILE_NAME)) ?
getSemanticModel(tmpDir.resolve(CLIENT_FILE_NAME)) :
getSemanticModel(tmpDir.resolve(SERVICE_FILE_NAME));
List<Symbol> symbols = semanticModel.moduleSymbols();
for (Symbol symbol : symbols) {
if (symbol.kind().equals(SymbolKind.TYPE_DEFINITION) || symbol.kind().equals(SymbolKind.ENUM)) {
List<Location> references = semanticModel.references(symbol);
if (references.size() == 1) {
unusedTypeDefinitionNameList.add(symbol.getName().get());
} else if (references.size() == 2) {
if (symbol.kind().equals(SymbolKind.TYPE_DEFINITION)) {
TypeDefinitionSymbol typeDef = (TypeDefinitionSymbol) symbol;
handleCyclicType(unusedTypeDefinitionNameList, typeDef);
}
}
}
}
Expand All @@ -1021,6 +1034,29 @@ private static List<String> getUnusedTypeDefinitionNameList(Map<String, String>
return unusedTypeDefinitionNameList;
}

private static void handleCyclicType(List<String> unusedTypeDefinitionNameList, TypeDefinitionSymbol symbol) {
TypeDescKind typeDescKind = symbol.typeDescriptor().typeKind();
if (typeDescKind.equals(TypeDescKind.RECORD)) {
RecordTypeSymbol recordTypeSymbol = (RecordTypeSymbol) symbol.typeDescriptor();
Map<String, RecordFieldSymbol> fields = recordTypeSymbol.fieldDescriptors();
Collection<RecordFieldSymbol> values = fields.values();
boolean isCyclic = false;
for (RecordFieldSymbol field: values) {
if (field.typeDescriptor().getName().isPresent()) {
if (field.typeDescriptor().getName().get().trim().equals(symbol.getName().get().trim())) {
isCyclic = true;
break;
}
}
}
if (isCyclic) {
unusedTypeDefinitionNameList.add(symbol.getName().get());
}
}
//TODO: Handle cyclic type for other ex: array after this fix
//https://github.com/ballerina-platform/ballerina-lang/issues/36442
}

private static void writeFilesTemp(Map<String, String> srcFiles, Path tmpDir) throws IOException {
srcFiles.put(BALLERINA_TOML, BALLERINA_TOML_CONTENT);
PrintWriter writer = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,3 @@ public type User record {
string firstName?;
string lastName?;
};

public type PetForm record {
string userName;
string firstName?;
string lastName?;
};

public type Pet record {
string userName;
string firstName?;
string lastName?;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,9 @@ public type BadRequestInline_response_400 record {|
Inline_response_400 body;
|};

public type User record {
string userName;
string firstName?;
string lastName?;
};

public type PetForm record {
string userName;
string firstName?;
string lastName?;
};

public type Inline_response_400 record {
# The error ID.
int id?;
# The error name.
string errorType?;
};

public type Pet record {
string userName;
string firstName?;
string lastName?;
};

0 comments on commit e62063c

Please sign in to comment.