Skip to content

Commit

Permalink
Refactor flatten logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Sep 10, 2024
1 parent 304dcf3 commit 42d4c6b
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions openapi-cli/src/main/java/io/ballerina/openapi/cmd/Flatten.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import io.swagger.v3.parser.util.InlineModelResolver;
import picocli.CommandLine;

import java.io.IOException;
Expand All @@ -20,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static io.ballerina.openapi.cmd.CmdConstants.JSON_EXTENSION;
import static io.ballerina.openapi.cmd.CmdConstants.OPENAPI_FLATTEN_CMD;
Expand Down Expand Up @@ -123,7 +125,7 @@ private void populateInputOptions() {
}

if (Objects.isNull(name)) {
name = "flatten_openapi";
name = "flattened_openapi";
}

if (Objects.nonNull(outputPath)) {
Expand All @@ -146,12 +148,23 @@ private void generateFlattenOpenAPI() {
return;
}

SwaggerParseResult parseResult = getOpenAPIWithFlattenOption(openAPIFileContent);
Optional<OpenAPI> openAPIOptional = getFlattenOpenAPI(openAPIFileContent);
if (openAPIOptional.isEmpty()) {
exitError();
return;
}
writeFlattenOpenAPIFile(openAPIOptional.get());
}

private Optional<OpenAPI> getFlattenOpenAPI(String openAPIFileContent) {
// Read the contents of the file with default parser options
// Flattening will be done after filtering the operations
SwaggerParseResult parseResult = new OpenAPIParser().readContents(openAPIFileContent, null,
new ParseOptions());
if (!parseResult.getMessages().isEmpty()) {
if (parseResult.getMessages().contains(UNSUPPORTED_OPENAPI_VERSION_PARSER_MESSAGE)) {
errorStream.println(ERROR_UNSUPPORTED_OPENAPI_VERSION);
exitError();
return;
return Optional.empty();
}
}

Expand All @@ -162,20 +175,14 @@ private void generateFlattenOpenAPI() {
errorStream.println(FOUND_PARSER_DIAGNOSTICS);
parseResult.getMessages().forEach(errorStream::println);
}
exitError();
return;
return Optional.empty();
}

filterOpenAPIOperations(openAPI);
writeFlattenOpenAPIFile(openAPI);
}

private static SwaggerParseResult getOpenAPIWithFlattenOption(String openAPIFileContent) {
ParseOptions parseOptions = new ParseOptions();
parseOptions.setFlatten(true);
parseOptions.setCamelCaseFlattenNaming(true);
parseOptions.setFlattenComposedSchemas(true);
return new OpenAPIParser().readContents(openAPIFileContent, null, parseOptions);
// Flatten the OpenAPI definition with `flattenComposedSchemas: true` and `camelCaseFlattenNaming: true`
InlineModelResolver inlineModelResolver = new InlineModelResolver(true, true);
inlineModelResolver.flatten(openAPI);
return Optional.of(openAPI);
}

private void writeFlattenOpenAPIFile(OpenAPI openAPI) {
Expand Down Expand Up @@ -204,6 +211,7 @@ private void filterOpenAPIOperations(OpenAPI openAPI) {
return;
}

// Remove the operations which are not present in the filter
openAPI.getPaths().forEach((path, pathItem) -> pathItem.readOperationsMap()
.forEach((httpMethod, operation) -> {
if (!filter.getOperations().contains(operation.getOperationId()) &&
Expand All @@ -212,6 +220,15 @@ private void filterOpenAPIOperations(OpenAPI openAPI) {
}
})
);

// Remove the paths which do not have any operations after filtering
List<String> pathsToRemove = new ArrayList<>();
openAPI.getPaths().forEach((path, pathItem) -> {
if (pathItem.readOperationsMap().isEmpty()) {
pathsToRemove.add(path);
}
});
pathsToRemove.forEach(openAPI.getPaths()::remove);
}

private Filter getFilter() {
Expand Down

0 comments on commit 42d4c6b

Please sign in to comment.