Skip to content

Commit

Permalink
Merge pull request #1803 from ballerina-platform/sanitize-fixes
Browse files Browse the repository at this point in the history
Enable resolver for sanitize sub command
  • Loading branch information
TharmiganK authored Dec 13, 2024
2 parents 7a1861e + da0d6fb commit 63df1bd
Show file tree
Hide file tree
Showing 6 changed files with 3,822 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ public class Flatten extends SubCmdBase {

private static final String ERROR_OCCURRED_WHILE_GENERATING_SCHEMA_NAMES = "ERROR: error occurred while " +
"generating schema names";
private static final String INFO_MSG_PREFIX = "Flattened";

public Flatten() {
super(CommandType.FLATTEN);
super(CommandType.FLATTEN, INFO_MSG_PREFIX);
}

public Flatten(PrintStream errorStream, boolean exitWhenFinish) {
super(CommandType.FLATTEN, errorStream, exitWhenFinish);
super(CommandType.FLATTEN, INFO_MSG_PREFIX, errorStream, exitWhenFinish);
}

@Override
Expand All @@ -67,7 +68,7 @@ public String getDefaultFileName() {

@Override
public Optional<OpenAPI> generate(String openAPIFileContent) {
Optional<OpenAPI> filteredOpenAPI = getFilteredOpenAPI(openAPIFileContent);
Optional<OpenAPI> filteredOpenAPI = getFilteredOpenAPI(openAPIFileContent, false);
if (filteredOpenAPI.isEmpty()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
)
public class Sanitize extends SubCmdBase {

private static final String INFO_MSG_PREFIX = "Sanitized";

public Sanitize() {
super(CommandType.SANITIZE);
super(CommandType.SANITIZE, INFO_MSG_PREFIX);
}

public Sanitize(PrintStream errorStream, boolean exitWhenFinish) {
super(CommandType.SANITIZE, errorStream, exitWhenFinish);
super(CommandType.SANITIZE, INFO_MSG_PREFIX, errorStream, exitWhenFinish);
}

@Override
Expand All @@ -53,7 +55,7 @@ public String getDefaultFileName() {

@Override
public Optional<OpenAPI> generate(String openAPIFileContent) {
Optional<OpenAPI> filteredOpenAPI = getFilteredOpenAPI(openAPIFileContent);
Optional<OpenAPI> filteredOpenAPI = getFilteredOpenAPI(openAPIFileContent, true);
return filteredOpenAPI.flatMap(this::sanitizeOpenAPI);
}

Expand Down
21 changes: 13 additions & 8 deletions openapi-cli/src/main/java/io/ballerina/openapi/cmd/SubCmdBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String getName() {
private static final String COMMAND_IDENTIFIER = "openapi-%s";
private static final String COMMA = ",";

private static final String INFO_OUTPUT_WRITTEN_MSG = "INFO: %sed OpenAPI definition file was successfully" +
private static final String INFO_OUTPUT_WRITTEN_MSG = "INFO: %s OpenAPI definition file was successfully" +
" written to: %s%n";
private static final String WARNING_INVALID_OUTPUT_FORMAT = "WARNING: invalid output format. The output format" +
" should be either \"json\" or \"yaml\".Defaulting to format of the input file";
Expand All @@ -93,7 +93,7 @@ public String getName() {
"parsing the OpenAPI definition file";
protected static final String FOUND_PARSER_DIAGNOSTICS = "found the following parser diagnostic messages:";
private static final String ERROR_OCCURRED_WHILE_WRITING_THE_OUTPUT_OPENAPI_FILE = "ERROR: error occurred while " +
"writing the %sed OpenAPI definition file%n";
"writing the %s OpenAPI definition file%n";
private static final String WARNING_SWAGGER_V2_FOUND = "WARNING: Swagger version 2.0 found in the OpenAPI " +
"definition. The generated OpenAPI definition will be in OpenAPI version 3.0.x";

Expand All @@ -103,6 +103,7 @@ public String getName() {
private Path targetPath = Paths.get(System.getProperty("user.dir"));
private boolean exitWhenFinish = true;
private final CommandType cmdType;
private final String infoMsgPrefix;

@CommandLine.Option(names = {"-h", "--help"}, hidden = true)
public boolean helpFlag;
Expand All @@ -125,14 +126,16 @@ public String getName() {
@CommandLine.Option(names = {"--operations"}, description = "Operations that need to be included when sanitizing.")
public String operations;

protected SubCmdBase(CommandType cmdType) {
protected SubCmdBase(CommandType cmdType, String infoMsgPrefix) {
this.cmdType = cmdType;
this.infoMsgPrefix = infoMsgPrefix;
}

protected SubCmdBase(CommandType cmdType, PrintStream errorStream, boolean exitWhenFinish) {
protected SubCmdBase(CommandType cmdType, String infoMsgPrefix, PrintStream errorStream, boolean exitWhenFinish) {
this.cmdType = cmdType;
this.errorStream = errorStream;
this.exitWhenFinish = exitWhenFinish;
this.infoMsgPrefix = infoMsgPrefix;
}

public void printHelpText() {
Expand Down Expand Up @@ -198,11 +201,13 @@ public Optional<OpenAPI> getFlattenOpenAPI(OpenAPI openAPI) {
return Optional.of(openAPI);
}

public Optional<OpenAPI> getFilteredOpenAPI(String openAPIFileContent) {
public Optional<OpenAPI> getFilteredOpenAPI(String openAPIFileContent, boolean enableResolver) {
// Read the contents of the file with default parser options
// Flattening will be done after filtering the operations
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolve(enableResolver);
SwaggerParseResult parserResult = new OpenAPIParser().readContents(openAPIFileContent, null,
new ParseOptions());
parseOptions);
if (!parserResult.getMessages().isEmpty() &&
parserResult.getMessages().contains(UNSUPPORTED_OPENAPI_VERSION_PARSER_MESSAGE)) {
errorStream.println(ERROR_UNSUPPORTED_OPENAPI_VERSION);
Expand Down Expand Up @@ -287,9 +292,9 @@ private void writeGeneratedOpenAPIFile(OpenAPI openAPI) {
try {
CodegenUtils.writeFile(targetPath.resolve(outputFileNameWithExt),
outputFileNameWithExt.endsWith(JSON_EXTENSION) ? Json.pretty(openAPI) : Yaml.pretty(openAPI));
infoStream.printf(INFO_OUTPUT_WRITTEN_MSG, cmdType.getName(), targetPath.resolve(outputFileNameWithExt));
infoStream.printf(INFO_OUTPUT_WRITTEN_MSG, infoMsgPrefix, targetPath.resolve(outputFileNameWithExt));
} catch (IOException exception) {
errorStream.printf(ERROR_OCCURRED_WHILE_WRITING_THE_OUTPUT_OPENAPI_FILE, cmdType.getName());
errorStream.printf(ERROR_OCCURRED_WHILE_WRITING_THE_OUTPUT_OPENAPI_FILE, infoMsgPrefix);
exitError();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,17 @@ public void testSanitizeCmdWithComposedSchema() throws IOException {
compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.json"));
}

@Test(description = "Test openapi sanitize sub command with unresolved parameter schema")
public void testSanitizeCmdWithUnresolvedParameterSchema() throws IOException {
Path expectedFilePath = resourceDir.resolve(
Paths.get("cmd/sanitize/sanitized_openapi_unresolved_expected.yaml"));
String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi_unresolved.yaml", "-o", tmpDir.toString()};
Sanitize sanitize = new Sanitize();
new CommandLine(sanitize).parseArgs(args);
sanitize.execute();
compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.yaml"));
}

@AfterTest
public void clean() {
System.setErr(null);
Expand Down
Loading

0 comments on commit 63df1bd

Please sign in to comment.