diff --git a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Align.java similarity index 64% rename from openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java rename to openapi-cli/src/main/java/io/ballerina/openapi/cmd/Align.java index 66dcc2dd5..2fd2dd305 100644 --- a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Sanitize.java +++ b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/Align.java @@ -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 generate(String openAPIFileContent) { Optional filteredOpenAPI = getFilteredOpenAPI(openAPIFileContent, true); - return filteredOpenAPI.flatMap(this::sanitizeOpenAPI); + return filteredOpenAPI.flatMap(this::alignOpenAPI); } - private Optional sanitizeOpenAPI(OpenAPI openAPI) { - OASModifier oasSanitizer = new OASModifier(); + private Optional 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(); diff --git a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java index 62ae58d28..6cca305d8 100644 --- a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java +++ b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/OpenApiCmd.java @@ -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"; diff --git a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/SubCmdBase.java b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/SubCmdBase.java index 075283f19..d7245e3cd 100644 --- a/openapi-cli/src/main/java/io/ballerina/openapi/cmd/SubCmdBase.java +++ b/openapi-cli/src/main/java/io/ballerina/openapi/cmd/SubCmdBase.java @@ -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 @@ -61,7 +61,7 @@ public abstract class SubCmdBase implements BLauncherCmd { public enum CommandType { FLATTEN("flatten"), - SANITIZE("sanitize"); + ALIGN("align"); private final String name; @@ -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.") diff --git a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-align.help b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-align.help new file mode 100644 index 000000000..83d04e810 --- /dev/null +++ b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-align.help @@ -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] + [-o | --output] + [-n | --name] + [-f | --format] [json|yaml] + [-t | --tags] + [--operations] + +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 + 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 + 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 + 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 + This is an optional input. The aligned OpenAPI contract will only + have the operations with the given tags. + + --operations + 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 diff --git a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help b/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help deleted file mode 100644 index cbd6607e6..000000000 --- a/openapi-cli/src/main/resources/cli-help/ballerina-openapi-sanitize.help +++ /dev/null @@ -1,56 +0,0 @@ -NAME - bal openapi sanitize - Sanitize the OpenAPI contract file according to - the best practices of Ballerina. - -SYNOPSIS - bal openapi sanitize [-i | --input] - [-o | --output] - [-n | --name] - [-f | --format] [json|yaml] - [-t | --tags] - [--operations] - -DESCRIPTION - Sanitize 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 - This is a mandatory input. The given OpenAPI contract will be sanitized. - The OpenAPI contract can be either a YAML or a JSON. - - -o, --output - This is an optional input. The given output file path will be used to - save the sanitized OpenAPI contract. The default output file path is - the executed directory. - - -n, --name - This is an optional input. The given name will be used to save the - sanitized OpenAPI contract. The default name is `sanitized_openapi`. - - -f, --format [json|yaml] - This is an optional input. The sanitized 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 - This is an optional input. The sanitized OpenAPI contract will only - have the operations with the given tags. - - --operations - This is an optional input. The sanitized OpenAPI contract will only - have the given operations. - -EXAMPLES - Sanitize the `service.yaml` OpenAPI contract file. - $ bal openapi sanitize -i service.yaml - - Sanitize the `service.yaml` OpenAPI contract file and save it as - `sanitized_svc.json` file. - $ bal openapi sanitize -i hello.yaml -n sanitized_svc -f json - - Sanitize the `service.json` OpenAPI contract file by filtering the - operations with the `service` tag. - $ bal openapi sanitize -i service.json -t service diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/NegativeCmdTests.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/NegativeCmdTests.java index 60522ea60..c98dadd81 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/NegativeCmdTests.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/NegativeCmdTests.java @@ -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")); diff --git a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java index 1d0e19227..08a5134e9 100644 --- a/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java +++ b/openapi-cli/src/test/java/io/ballerina/openapi/cmd/OpenAPICmdTest.java @@ -1025,118 +1025,118 @@ public void testFlattenCmdWithComposedSchema() throws IOException { compareFiles(expectedFilePath, tmpDir.resolve("flattened_openapi.yaml")); } - @Test(description = "Test openapi sanitize sub command with default options with the json file") - public void testSanitizeCmdDefaultJson() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_expected.json")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.json", "-o", tmpDir.toString()}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.json")); + @Test(description = "Test openapi align sub command with default options with the json file") + public void testAlignCmdDefaultJson() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_expected.json")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.json", "-o", tmpDir.toString()}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.json")); } - @Test(description = "Test openapi sanitize sub command with default options with the yaml file") - public void testSanitizeCmdDefaultYaml() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_expected.yaml")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.yaml", "-o", tmpDir.toString()}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.yaml")); + @Test(description = "Test openapi align sub command with default options with the yaml file") + public void testAlignCmdDefaultYaml() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_expected.yaml")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.yaml", "-o", tmpDir.toString()}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.yaml")); } - @Test(description = "Test openapi sanitize sub command with Swagger 2.0") - public void testSanitizeCmdWithSwaggerV2() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_2.0_expected.yaml")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi_2.0.yaml", "-o", tmpDir.toString()}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.yaml")); + @Test(description = "Test openapi align sub command with Swagger 2.0") + public void testAlignCmdWithSwaggerV2() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_2.0_expected.yaml")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi_2.0.yaml", "-o", tmpDir.toString()}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.yaml")); } - @Test(description = "Test openapi sanitize sub command with OpenAPI 3.0.0") - public void testSanitizeCmdWithOpenAPIV3_0_0() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_3.0.0_expected.yaml")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi_3.0.0.yaml", "-o", tmpDir.toString()}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.yaml")); + @Test(description = "Test openapi align sub command with OpenAPI 3.0.0") + public void testAlignCmdWithOpenAPIV3_0_0() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_3.0.0_expected.yaml")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi_3.0.0.yaml", "-o", tmpDir.toString()}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.yaml")); } - @Test(description = "Test openapi sanitize sub command with the name option") - public void testSanitizeCmdName() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_expected.json")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.json", "-o", tmpDir.toString(), "-n", "sanitized"}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized.json")); + @Test(description = "Test openapi align sub command with the name option") + public void testAlignCmdName() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_expected.json")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.json", "-o", tmpDir.toString(), "-n", "aligned"}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned.json")); } - @Test(description = "Test openapi sanitize sub command with the json format option") - public void testSanitizeCmdJsonFormat() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_expected_1.json")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.yaml", "-o", tmpDir.toString(), "-f", "json"}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.json")); + @Test(description = "Test openapi align sub command with the json format option") + public void testAlignCmdJsonFormat() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_expected_1.json")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.yaml", "-o", tmpDir.toString(), "-f", "json"}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.json")); } - @Test(description = "Test openapi sanitize sub command with the yaml format option") - public void testSanitizeCmdYamlFormat() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_expected_1.yaml")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.json", "-o", tmpDir.toString(), "-f", "yaml"}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.yaml")); + @Test(description = "Test openapi align sub command with the yaml format option") + public void testAlignCmdYamlFormat() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_expected_1.yaml")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.json", "-o", tmpDir.toString(), "-f", "yaml"}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.yaml")); } - @Test(description = "Test openapi sanitize sub command with the tags option") - public void testSanitizeCmdTags() throws IOException { + @Test(description = "Test openapi align sub command with the tags option") + public void testAlignCmdTags() throws IOException { Path expectedFilePath = resourceDir.resolve( - Paths.get("cmd/sanitize/sanitized_openapi_expected_albums.json")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.json", "-o", tmpDir.toString(), "-t", "albums"}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.json")); + Paths.get("cmd/align/aligned_openapi_expected_albums.json")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.json", "-o", tmpDir.toString(), "-t", "albums"}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.json")); } - @Test(description = "Test openapi sanitize sub command with the operations option") - public void testSanitizeCmdOperations() throws IOException { + @Test(description = "Test openapi align sub command with the operations option") + public void testAlignCmdOperations() throws IOException { Path expectedFilePath = resourceDir.resolve( - Paths.get("cmd/sanitize/sanitized_openapi_expected_operations.json")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi.json", "-o", tmpDir.toString(), "--operations", + Paths.get("cmd/align/aligned_openapi_expected_operations.json")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi.json", "-o", tmpDir.toString(), "--operations", "getAlbumById,getAlbums"}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.json")); + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.json")); } - @Test(description = "Test openapi sanitize sub command with composed schema") - public void testSanitizeCmdWithComposedSchema() throws IOException { - Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/sanitize/sanitized_openapi_composed_schema.json")); - String[] args = {"-i", resourceDir + "/cmd/sanitize/openapi_composed_schema.json", "-o", tmpDir.toString()}; - Sanitize sanitize = new Sanitize(); - new CommandLine(sanitize).parseArgs(args); - sanitize.execute(); - compareFiles(expectedFilePath, tmpDir.resolve("sanitized_openapi.json")); + @Test(description = "Test openapi align sub command with composed schema") + public void testAlignCmdWithComposedSchema() throws IOException { + Path expectedFilePath = resourceDir.resolve(Paths.get("cmd/align/aligned_openapi_composed_schema.json")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi_composed_schema.json", "-o", tmpDir.toString()}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.json")); } - @Test(description = "Test openapi sanitize sub command with unresolved parameter schema") - public void testSanitizeCmdWithUnresolvedParameterSchema() throws IOException { + @Test(description = "Test openapi align sub command with unresolved parameter schema") + public void testAlignCmdWithUnresolvedParameterSchema() 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")); + Paths.get("cmd/align/aligned_openapi_unresolved_expected.yaml")); + String[] args = {"-i", resourceDir + "/cmd/align/openapi_unresolved.yaml", "-o", tmpDir.toString()}; + Align align = new Align(); + new CommandLine(align).parseArgs(args); + align.execute(); + compareFiles(expectedFilePath, tmpDir.resolve("aligned_ballerina_openapi.yaml")); } @AfterTest diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_2.0_expected.yaml b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_2.0_expected.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_2.0_expected.yaml rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_2.0_expected.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_3.0.0_expected.yaml b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_3.0.0_expected.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_3.0.0_expected.yaml rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_3.0.0_expected.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_composed_schema.json b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_composed_schema.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_composed_schema.json rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_composed_schema.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected.json b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected.json rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected.yaml b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected.yaml rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_1.json b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_1.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_1.json rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_1.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_1.yaml b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_1.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_1.yaml rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_1.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_albums.json b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_albums.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_albums.json rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_albums.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_operations.json b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_operations.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_expected_operations.json rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_expected_operations.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_unresolved_expected.yaml b/openapi-cli/src/test/resources/cmd/align/aligned_openapi_unresolved_expected.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/sanitized_openapi_unresolved_expected.yaml rename to openapi-cli/src/test/resources/cmd/align/aligned_openapi_unresolved_expected.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi.json b/openapi-cli/src/test/resources/cmd/align/openapi.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi.json rename to openapi-cli/src/test/resources/cmd/align/openapi.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi.yaml b/openapi-cli/src/test/resources/cmd/align/openapi.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi.yaml rename to openapi-cli/src/test/resources/cmd/align/openapi.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi_2.0.yaml b/openapi-cli/src/test/resources/cmd/align/openapi_2.0.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi_2.0.yaml rename to openapi-cli/src/test/resources/cmd/align/openapi_2.0.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi_3.0.0.yaml b/openapi-cli/src/test/resources/cmd/align/openapi_3.0.0.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi_3.0.0.yaml rename to openapi-cli/src/test/resources/cmd/align/openapi_3.0.0.yaml diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi_composed_schema.json b/openapi-cli/src/test/resources/cmd/align/openapi_composed_schema.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi_composed_schema.json rename to openapi-cli/src/test/resources/cmd/align/openapi_composed_schema.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi_invalid.json b/openapi-cli/src/test/resources/cmd/align/openapi_invalid.json similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi_invalid.json rename to openapi-cli/src/test/resources/cmd/align/openapi_invalid.json diff --git a/openapi-cli/src/test/resources/cmd/sanitize/openapi_unresolved.yaml b/openapi-cli/src/test/resources/cmd/align/openapi_unresolved.yaml similarity index 100% rename from openapi-cli/src/test/resources/cmd/sanitize/openapi_unresolved.yaml rename to openapi-cli/src/test/resources/cmd/align/openapi_unresolved.yaml diff --git a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java index 677686bcb..a8417ab97 100644 --- a/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java +++ b/openapi-core/src/main/java/io/ballerina/openapi/core/generators/common/OASModifier.java @@ -52,7 +52,7 @@ import static io.ballerina.openapi.core.generators.common.GeneratorConstants.QUERY; /** - * This class is to contain the sanitized utils for naming. + * This class is to contain the aligned utils for naming. * * @since 2.1.0 */ @@ -267,10 +267,10 @@ private static OpenAPI modifyOASWithSchemaName(OpenAPI openapi, Map messages = parseResult.getMessages(); if (Objects.nonNull(messages) && !messages.isEmpty()) { - outErrorStream.println("Schema name sanitization contains errors: "); + outErrorStream.println("Aligning schema name for Ballerina contains errors: "); messages.forEach(outErrorStream::println); } - throw new BallerinaOpenApiException("Failed to generate the sanitized OpenAPI specification. Please " + + throw new BallerinaOpenApiException("Failed to align the OpenAPI specification for Ballerina. Please " + "consider generating the client/service without the sanitization option."); } return openAPI; @@ -423,20 +423,20 @@ private static void addBallerinaNameExtension(Parameter parameter) { return; } - String sanitizedName = getValidNameForParameter(parameterName); - if (parameterName.equals(sanitizedName)) { + String alignedName = getValidNameForParameter(parameterName); + if (parameterName.equals(alignedName)) { return; } if (Objects.isNull(parameter.getExtensions())) { parameter.setExtensions(new HashMap<>()); } - parameter.getExtensions().put(BALLERINA_NAME_EXT, sanitizedName); + parameter.getExtensions().put(BALLERINA_NAME_EXT, alignedName); } private static Schema getSchemaWithBallerinaNameExtension(String propertyName, Schema propertySchema) { - String sanitizedPropertyName = getValidNameForParameter(propertyName); - if (propertyName.equals(sanitizedPropertyName)) { + String alignedPropertyName = getValidNameForParameter(propertyName); + if (propertyName.equals(alignedPropertyName)) { return propertySchema; } @@ -451,7 +451,7 @@ private static Schema getSchemaWithBallerinaNameExtension(String propertyName, S if (Objects.isNull(propertySchema.getExtensions())) { propertySchema.setExtensions(new HashMap<>()); } - propertySchema.getExtensions().put(BALLERINA_NAME_EXT, sanitizedPropertyName); + propertySchema.getExtensions().put(BALLERINA_NAME_EXT, alignedPropertyName); return propertySchema; }