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

Change the OpenAPI Tool sanitize Sub Command Name into align #1809

Merged
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 @@ -26,43 +26,43 @@
import java.util.Optional;

/**
* Main class to implement "sanitize" subcommand which is used to flatten and sanitize the OpenAPI definition
* Main class to implement "align" subcommand which is used to flatten and align the OpenAPI definition
* by generating Ballerina friendly type schema names and Ballerina type name extensions.
*
* @since 2.2.0
*/
@CommandLine.Command(
name = "sanitize",
description = "Sanitize the OpenAPI definition by generating Ballerina friendly type schema names and " +
name = "align",
description = "Align the OpenAPI definition by generating Ballerina friendly type schema names and " +
"Ballerina type name extensions."
)
public class Sanitize extends SubCmdBase {
public class Align extends SubCmdBase {

private static final String INFO_MSG_PREFIX = "Sanitized";
private static final String INFO_MSG_PREFIX = "Aligned";

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

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

@Override
public String getDefaultFileName() {
return "sanitized_openapi";
return "aligned_ballerina_openapi";
}

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

private Optional<OpenAPI> sanitizeOpenAPI(OpenAPI openAPI) {
OASModifier oasSanitizer = new OASModifier();
private Optional<OpenAPI> alignOpenAPI(OpenAPI openAPI) {
OASModifier oasAligner = new OASModifier();
try {
return Optional.of(oasSanitizer.modifyWithBallerinaConventions(openAPI));
return Optional.of(oasAligner.modifyWithBallerinaConventions(openAPI));
} catch (BallerinaOpenApiException exp) {
printError("ERROR: %s".formatted(exp.getMessage()));
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
@CommandLine.Command(
name = "openapi",
description = "Generate the Ballerina sources for a given OpenAPI definition and vice versa.",
subcommands = {Add.class, Flatten.class, Sanitize.class}
subcommands = {Add.class, Flatten.class, Align.class}
)
public class OpenApiCmd implements BLauncherCmd {
private static final String CMD_NAME = "openapi";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import static io.ballerina.openapi.service.mapper.utils.CodegenUtils.resolveContractFileName;

/**
* Abstract class representing the {@link Flatten} and {@link Sanitize} sub command
* Abstract class representing the {@link Flatten} and {@link Align} sub command
* implementations.
*
* @since 2.2.0
Expand All @@ -61,7 +61,7 @@ public abstract class SubCmdBase implements BLauncherCmd {

public enum CommandType {
FLATTEN("flatten"),
SANITIZE("sanitize");
ALIGN("align");

private final String name;

Expand Down Expand Up @@ -111,13 +111,13 @@ public String getName() {
@CommandLine.Option(names = {"-i", "--input"}, description = "OpenAPI definition file path.")
public String inputPath;

@CommandLine.Option(names = {"-o", "--output"}, description = "Location of the sanitized OpenAPI definition.")
@CommandLine.Option(names = {"-o", "--output"}, description = "Location of the aligned OpenAPI definition.")
private String outputPath;

@CommandLine.Option(names = {"-n", "--name"}, description = "Name of the sanitized OpenAPI definition file.")
@CommandLine.Option(names = {"-n", "--name"}, description = "Name of the aligned OpenAPI definition file.")
private String fileName;

@CommandLine.Option(names = {"-f", "--format"}, description = "Output format of the sanitized OpenAPI definition.")
@CommandLine.Option(names = {"-f", "--format"}, description = "Output format of the aligned OpenAPI definition.")
private String format;

@CommandLine.Option(names = {"-t", "--tags"}, description = "Tags that need to be considered when sanitizing.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
NAME
bal openapi align - Align the OpenAPI contract file according to
the best practices of Ballerina.

SYNOPSIS
bal openapi align [-i | --input] <openapi-contract-file-path>
[-o | --output] <output-file-path>
[-n | --name] <generated-file-name>
[-f | --format] [json|yaml]
[-t | --tags] <tag-names>
[--operations] <operation-names>

DESCRIPTION
Align the OpenAPI contract file according to the best naming
practices of Ballerina. The Ballerina name extensions are added
to the schemas which can not be modified directly.


OPTIONS
-i, --input <openapi-contract-file-path>
This is a mandatory input. The given OpenAPI contract will be aligned.
The OpenAPI contract can be either a YAML or a JSON.

-o, --output <output-file-path>
This is an optional input. The given output file path will be used to
save the aligned OpenAPI contract. The default output file path is
the executed directory.

-n, --name <generated-file-name>
This is an optional input. The given name will be used to save the
aligned OpenAPI contract. The default name is `aligned_ballerina_openapi`.

-f, --format [json|yaml]
This is an optional input. The aligned OpenAPI contract will be
saved in the given format. The format can be either JSON or YAML.
The default format is same as the input file format.

-t, --tags <tag-names>
This is an optional input. The aligned OpenAPI contract will only
have the operations with the given tags.

--operations <operation-names>
This is an optional input. The aligned OpenAPI contract will only
have the given operations.

EXAMPLES
Align the `service.yaml` OpenAPI contract file.
$ bal openapi align -i service.yaml

Align the `service.yaml` OpenAPI contract file and save it as
`aligned_svc.json` file.
$ bal openapi align -i hello.yaml -n aligned_svc -f json

Align the `service.json` OpenAPI contract file by filtering the
operations with the `service` tag.
$ bal openapi align -i service.json -t service

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -98,67 +98,67 @@ public void testFlattenWithInputOpenAPIFileParsingIssues() throws IOException {
"\"json\" or \"yaml\".Defaulting to format of the input file"));
}

@Test(description = "Test without the input OpenAPI file in `sanitize` sub command")
public void testSanitizeWithOutInputOpenAPIFile() throws IOException {
@Test(description = "Test without the input OpenAPI file in `align` sub command")
public void testAlignWithOutInputOpenAPIFile() throws IOException {
String[] addArgs = {"-f", "json"};
Sanitize sanitize = new Sanitize(printStream, false);
new CommandLine(sanitize).parseArgs(addArgs);
sanitize.execute();
Align align = new Align(printStream, false);
new CommandLine(align).parseArgs(addArgs);
align.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("ERROR: an OpenAPI definition path is required to sanitize the OpenAPI " +
Assert.assertTrue(output.contains("ERROR: an OpenAPI definition path is required to align the OpenAPI " +
"definition"));
}

@Test(description = "Test with the invalid input OpenAPI file in `sanitize` sub command")
public void testSanitizeWithInvalidInputOpenAPIFile() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi-1.json"};
Sanitize sanitize = new Sanitize(printStream, false);
new CommandLine(sanitize).parseArgs(args);
sanitize.execute();
@Test(description = "Test with the invalid input OpenAPI file in `align` sub command")
public void testAlignWithInvalidInputOpenAPIFile() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/align/openapi-1.json"};
Align align = new Align(printStream, false);
new CommandLine(align).parseArgs(args);
align.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("ERROR: error occurred while reading the OpenAPI definition file"));
}

@Test(description = "Test with invalid invalid input OpenAPI file extension in `sanitize` sub command")
public void testSanitizeWithInvalidInputOpenAPIFileExtension() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.txt"};
Sanitize sanitize = new Sanitize(printStream, false);
new CommandLine(sanitize).parseArgs(args);
sanitize.execute();
@Test(description = "Test with invalid input OpenAPI file extension in `align` sub command")
public void testAlignWithInvalidInputOpenAPIFileExtension() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/align/openapi.txt"};
Align align = new Align(printStream, false);
new CommandLine(align).parseArgs(args);
align.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("ERROR: invalid input OpenAPI definition file extension. The OpenAPI " +
"definition file should be in YAML or JSON format"));
}

@Test(description = "Test with the invalid output OpenAPI file format in `sanitize` sub command")
public void testSanitizeWithInvalidOutputOpenAPIFileFormat() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.json", "-f", "txt", "-o", tmpDir.toString()};
Sanitize sanitize = new Sanitize(printStream, false);
new CommandLine(sanitize).parseArgs(args);
sanitize.execute();
@Test(description = "Test with the invalid output OpenAPI file format in `align` sub command")
public void testAlignWithInvalidOutputOpenAPIFileFormat() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/align/openapi.json", "-f", "txt", "-o", tmpDir.toString()};
Align align = new Align(printStream, false);
new CommandLine(align).parseArgs(args);
align.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("WARNING: invalid output format. The output format should be either " +
"\"json\" or \"yaml\".Defaulting to format of the input file"));
}

@Test(description = "Test with the input OpenAPI file in `sanitize` sub command which has parsing issues")
public void testSanitizeWithInputOpenAPIFileParsingIssues() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi_invalid.json", "-f", "txt", "-o",
@Test(description = "Test with the input OpenAPI file in `align` sub command which has parsing issues")
public void testAlignWithInputOpenAPIFileParsingIssues() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/align/openapi_invalid.json", "-f", "txt", "-o",
tmpDir.toString()};
Sanitize sanitize = new Sanitize(printStream, false);
new CommandLine(sanitize).parseArgs(args);
sanitize.execute();
Align align = new Align(printStream, false);
new CommandLine(align).parseArgs(args);
align.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("WARNING: invalid output format. The output format should be either " +
"\"json\" or \"yaml\".Defaulting to format of the input file"));
}

@Test(description = "Test with the input OpenAPI file with Swagger V2 in `sanitize` sub command")
public void testSanitizeWithSwaggerV2() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi_2.0.yaml", "-o", tmpDir.toString()};
Sanitize sanitize = new Sanitize(printStream, false);
new CommandLine(sanitize).parseArgs(args);
sanitize.execute();
@Test(description = "Test with the input OpenAPI file with Swagger V2 in `align` sub command")
public void testAlignWithSwaggerV2() throws IOException {
String[] args = {"-i", resourceDir + "/cmd/align/openapi_2.0.yaml", "-o", tmpDir.toString()};
Align align = new Align(printStream, false);
new CommandLine(align).parseArgs(args);
align.execute();
String output = readOutput(true);
Assert.assertTrue(output.contains("WARNING: Swagger version 2.0 found in the OpenAPI definition. The " +
"generated OpenAPI definition will be in OpenAPI version 3.0.x"));
Expand Down
Loading
Loading