Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable resolver for sanitize sub command #1803

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading