From 10be4d3de3fa8d47c08e41e1205f4ac2d39e1253 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 29 Nov 2024 14:46:14 -0300 Subject: [PATCH] remove operation id prefix (#873) (#880) Co-authored-by: yuhaibohotmail <48646226+yuhaibohotmail@users.noreply.github.com> --- .../generator/deployment/CodegenConfig.java | 1 + .../generator/deployment/SpecItemConfig.java | 7 ++ .../codegen/OpenApiGeneratorCodeGenBase.java | 9 ++ .../OpenApiClientGeneratorWrapper.java | 5 + client/integration-tests/pom.xml | 1 + .../remove-operationid-prefix/pom.xml | 91 +++++++++++++++ .../openapi-remove-operation-id-prefix.yaml | 108 ++++++++++++++++++ .../src/main/resources/application.properties | 2 + .../it/RemoveOperationIdPrefixTest.java | 33 ++++++ .../ROOT/pages/includes/getting-started.adoc | 7 ++ 10 files changed, 264 insertions(+) create mode 100644 client/integration-tests/remove-operationid-prefix/pom.xml create mode 100644 client/integration-tests/remove-operationid-prefix/src/main/openapi/openapi-remove-operation-id-prefix.yaml create mode 100644 client/integration-tests/remove-operationid-prefix/src/main/resources/application.properties create mode 100644 client/integration-tests/remove-operationid-prefix/src/test/java/io/quarkiverse/openapi/generator/it/RemoveOperationIdPrefixTest.java diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java index c913586e8..cb9c107e0 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java @@ -67,6 +67,7 @@ public enum ConfigName { USE_FIELD_NAME_IN_PART_FILENAME("use-field-name-in-part-filename"), ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute"), ADDITIONAL_REQUEST_ARGS("additional-request-args"), + REMOVE_OPERATION_ID_PREFIX("remove-operation-id-prefix"), BEAN_VALIDATION("use-bean-validation"); private final String name; diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecItemConfig.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecItemConfig.java index 36a42cbb4..9be9e42dd 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecItemConfig.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecItemConfig.java @@ -38,4 +38,11 @@ public class SpecItemConfig extends CommonItemConfig { */ @ConfigItem(name = "model-name-prefix") public Optional modelNamePrefix; + + /** + * Remove operation id prefix + */ + @ConfigItem(name = "remove-operation-id-prefix") + public Optional removeOperationIdPrefix; + } diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java index c2b0dd8f0..33d3a3f40 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java @@ -13,6 +13,7 @@ import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.INPUT_BASE_DIR; import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.MODEL_NAME_PREFIX; import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.MODEL_NAME_SUFFIX; +import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.REMOVE_OPERATION_ID_PREFIX; import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.TEMPLATE_BASE_DIR; import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.VALIDATE_SPEC; @@ -228,6 +229,9 @@ protected void generate(OpenApiGeneratorOptions options) { getModelNamePrefix(config, openApiFilePath) .ifPresent(generator::withModelNamePrefix); + getRemoveOperationIdPrefix(config, openApiFilePath) + .ifPresent(generator::withRemoveOperationIdPrefix); + getValues(config, openApiFilePath, CodegenConfig.ConfigName.MUTINY, Boolean.class) .ifPresent(generator::withMutiny); @@ -350,6 +354,11 @@ private Optional getModelNamePrefix(final Config config, final Path open .getOptionalValue(getSpecConfigName(MODEL_NAME_PREFIX, openApiFilePath), String.class); } + private Optional getRemoveOperationIdPrefix(final Config config, final Path openApiFilePath) { + return config + .getOptionalValue(getSpecConfigName(REMOVE_OPERATION_ID_PREFIX, openApiFilePath), Boolean.class); + } + private Optional getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) { return config.getOptionalValue(getGlobalConfigName(INPUT_BASE_DIR), String.class).map(baseDir -> { int srcIndex = sourceDir.toString().lastIndexOf("src"); diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java index e8cb162df..66bbacd85 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java @@ -280,6 +280,11 @@ public OpenApiClientGeneratorWrapper withModelNameSuffix(final String modelNameS return this; } + public OpenApiClientGeneratorWrapper withRemoveOperationIdPrefix(final Boolean removeOperationIdPrefix) { + this.configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix); + return this; + } + public OpenApiClientGeneratorWrapper withModelNamePrefix(final String modelNamePrefix) { this.configurator.setModelNamePrefix(modelNamePrefix); return this; diff --git a/client/integration-tests/pom.xml b/client/integration-tests/pom.xml index ef2670df3..f74befd08 100644 --- a/client/integration-tests/pom.xml +++ b/client/integration-tests/pom.xml @@ -33,6 +33,7 @@ part-filename polymorphism return-response + remove-operationid-prefix security simple skip-validation diff --git a/client/integration-tests/remove-operationid-prefix/pom.xml b/client/integration-tests/remove-operationid-prefix/pom.xml new file mode 100644 index 000000000..480678d50 --- /dev/null +++ b/client/integration-tests/remove-operationid-prefix/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + io.quarkiverse.openapi.generator + quarkus-openapi-generator-integration-tests + 3.0.0-SNAPSHOT + + + quarkus-openapi-generator-it-remove-operationid-prefix + Quarkus - Openapi Generator - Integration Tests - Client - remove operation id prefix + Example project for general usage with remove operation id prefix + + + + io.quarkiverse.openapi.generator + quarkus-openapi-generator + + + org.assertj + assertj-core + test + + + io.quarkus + quarkus-junit5 + test + + + + + + + io.quarkus + quarkus-maven-plugin + true + + + + build + generate-code + generate-code-tests + + + + + + + + + native-image + + + native + + + + + + maven-surefire-plugin + + ${native.surefire.skip} + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + ${project.build.directory}/${project.build.finalName}-runner + org.jboss.logmanager.LogManager + ${maven.home} + + + + + + + + + native + + + + + \ No newline at end of file diff --git a/client/integration-tests/remove-operationid-prefix/src/main/openapi/openapi-remove-operation-id-prefix.yaml b/client/integration-tests/remove-operationid-prefix/src/main/openapi/openapi-remove-operation-id-prefix.yaml new file mode 100644 index 000000000..270c78d9c --- /dev/null +++ b/client/integration-tests/remove-operationid-prefix/src/main/openapi/openapi-remove-operation-id-prefix.yaml @@ -0,0 +1,108 @@ +--- +openapi: 3.0.3 +info: + title: code-with-quarkus API + version: 1.0.0-SNAPSHOT +servers: +- url: http://localhost:8080 + description: Auto generated value +- url: http://0.0.0.0:8080 + description: Auto generated value +paths: + /users: + get: + tags: + - User Resource + description: Find All + operationId: UserResource_findAll + responses: + "200": + description: OK + content: + application/json: + schema: + type: array + items: + $ref: "#/components/schemas/User" + post: + tags: + - User Resource + description: Add + operationId: UserResource_add + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/User" + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/User" + /users/{id}: + get: + tags: + - User Resource + description: Find + operationId: UserResource_find + parameters: + - name: id + in: path + required: true + schema: + format: int32 + type: integer + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/User" + put: + tags: + - User Resource + description: Update + operationId: UserResource_update + parameters: + - name: id + in: path + required: true + schema: + format: int32 + type: integer + requestBody: + content: + application/json: + schema: + $ref: "#/components/schemas/User" + responses: + "204": + description: No Content + delete: + tags: + - User Resource + description: Delete + operationId: UserResource_delete + parameters: + - name: id + in: path + required: true + schema: + format: int32 + type: integer + responses: + "204": + description: No Content +components: + schemas: + User: + type: object + properties: + id: + format: int32 + type: integer + name: + type: string diff --git a/client/integration-tests/remove-operationid-prefix/src/main/resources/application.properties b/client/integration-tests/remove-operationid-prefix/src/main/resources/application.properties new file mode 100644 index 000000000..cef3041dd --- /dev/null +++ b/client/integration-tests/remove-operationid-prefix/src/main/resources/application.properties @@ -0,0 +1,2 @@ +quarkus.rest-client.quarkus_simple_openapi_yaml.url=http://localhost:8080 +quarkus.openapi-generator.codegen.spec.openapi_remove_operation_id_prefix_yaml.remove-operation-id-prefix=true diff --git a/client/integration-tests/remove-operationid-prefix/src/test/java/io/quarkiverse/openapi/generator/it/RemoveOperationIdPrefixTest.java b/client/integration-tests/remove-operationid-prefix/src/test/java/io/quarkiverse/openapi/generator/it/RemoveOperationIdPrefixTest.java new file mode 100644 index 000000000..64d0b9833 --- /dev/null +++ b/client/integration-tests/remove-operationid-prefix/src/test/java/io/quarkiverse/openapi/generator/it/RemoveOperationIdPrefixTest.java @@ -0,0 +1,33 @@ +package io.quarkiverse.openapi.generator.it; + +import static org.assertj.core.api.Assertions.assertThatCode; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class RemoveOperationIdPrefixTest { + + String apiClassName = "org.openapi.quarkus.openapi_remove_operation_id_prefix_yaml.api.UserResourceApi"; + String modelClassName = "org.openapi.quarkus.openapi_remove_operation_id_prefix_yaml.model.User"; + + @Test + void apiIsBeingGenerated() { + assertThatCode(() -> Class.forName(apiClassName).getMethod("find", Integer.class)) + .doesNotThrowAnyException(); + + assertThatCode(() -> Class.forName(apiClassName).getMethod("findAll")) + .doesNotThrowAnyException(); + + assertThatCode(() -> Class.forName(apiClassName).getMethod("add", Class.forName(modelClassName))) + .doesNotThrowAnyException(); + + assertThatCode(() -> Class.forName(apiClassName).getMethod("update", Integer.class, Class.forName(modelClassName))) + .doesNotThrowAnyException(); + + assertThatCode(() -> Class.forName(apiClassName).getMethod("delete", Integer.class)) + .doesNotThrowAnyException(); + + } +} diff --git a/docs/modules/ROOT/pages/includes/getting-started.adoc b/docs/modules/ROOT/pages/includes/getting-started.adoc index e1cf60fa9..25296f58a 100644 --- a/docs/modules/ROOT/pages/includes/getting-started.adoc +++ b/docs/modules/ROOT/pages/includes/getting-started.adoc @@ -88,6 +88,13 @@ quarkus.openapi-generator.codegen.spec.petstore_json.model-name-suffix=CustomMod quarkus.openapi-generator.codegen.spec.petstore_json.model-name-prefix=CustomModelPrefix ---- +You can remove operationId prefix (e.g. User_findAll=> findAll). To do that, you must define the following properties: + +[source,properties] +---- +quarkus.openapi-generator.codegen.spec.petstore_json.remove-operation-id-prefix=true +---- + The same way you can add any additional annotations to the generated api files with `additional-api-type-annotations`. Given you want to include Foo and Bar annotations, you must define additional-api-type-annotations as: [source,properties]