Skip to content

Commit

Permalink
Improve remove operation id prefix (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhaibohotmail authored Dec 3, 2024
1 parent babae9b commit 0719839
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public enum ConfigName {
ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute"),
ADDITIONAL_REQUEST_ARGS("additional-request-args"),
REMOVE_OPERATION_ID_PREFIX("remove-operation-id-prefix"),
REMOVE_OPERATION_ID_PREFIX_DELIMITER("remove-operation-id-prefix-delimiter"),
REMOVE_OPERATION_ID_PREFIX_COUNT("remove-operation-id-prefix-count"),
BEAN_VALIDATION("use-bean-validation");

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public class SpecItemConfig extends CommonItemConfig {
* Remove operation id prefix
*/
@ConfigItem(name = "remove-operation-id-prefix")
public Optional<String> removeOperationIdPrefix;
public Optional<Boolean> removeOperationIdPrefix;

/**
* Remove operation id prefix
*/
@ConfigItem(name = "remove-operation-id-prefix-delimiter")
public Optional<String> removeOperationIdPrefixDelimiter;

/**
* Remove operation id prefix
*/
@ConfigItem(name = "remove-operation-id-prefix-count")
public Optional<Integer> removeOperationIdPrefixCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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.REMOVE_OPERATION_ID_PREFIX_COUNT;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.REMOVE_OPERATION_ID_PREFIX_DELIMITER;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.TEMPLATE_BASE_DIR;
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.ConfigName.VALIDATE_SPEC;

Expand Down Expand Up @@ -232,6 +234,12 @@ protected void generate(OpenApiGeneratorOptions options) {
getRemoveOperationIdPrefix(config, openApiFilePath)
.ifPresent(generator::withRemoveOperationIdPrefix);

getRemoveOperationIdPrefixDelimiter(config, openApiFilePath)
.ifPresent(generator::withRemoveOperationIdPrefixDelimiter);

getRemoveOperationIdPrefixCount(config, openApiFilePath)
.ifPresent(generator::withRemoveOperationIdPrefixCount);

getValues(config, openApiFilePath, CodegenConfig.ConfigName.MUTINY, Boolean.class)
.ifPresent(generator::withMutiny);

Expand Down Expand Up @@ -362,6 +370,16 @@ private Optional<Boolean> getRemoveOperationIdPrefix(final Config config, final
.getOptionalValue(getSpecConfigName(REMOVE_OPERATION_ID_PREFIX, openApiFilePath), Boolean.class);
}

private Optional<String> getRemoveOperationIdPrefixDelimiter(final Config config, final Path openApiFilePath) {
return config
.getOptionalValue(getSpecConfigName(REMOVE_OPERATION_ID_PREFIX_DELIMITER, openApiFilePath), String.class);
}

private Optional<Integer> getRemoveOperationIdPrefixCount(final Config config, final Path openApiFilePath) {
return config
.getOptionalValue(getSpecConfigName(REMOVE_OPERATION_ID_PREFIX_COUNT, openApiFilePath), Integer.class);
}

private Optional<String> getInputBaseDirRelativeToModule(final Path sourceDir, final Config config) {
return config.getOptionalValue(getGlobalConfigName(INPUT_BASE_DIR), String.class).map(baseDir -> {
int srcIndex = sourceDir.toString().lastIndexOf("src");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ public OpenApiClientGeneratorWrapper withRemoveOperationIdPrefix(final Boolean r
return this;
}

public OpenApiClientGeneratorWrapper withRemoveOperationIdPrefixDelimiter(final String removeOperationIdPrefixDelimiter) {
this.configurator.addAdditionalProperty("removeOperationIdPrefixDelimiter", removeOperationIdPrefixDelimiter);
return this;
}

public OpenApiClientGeneratorWrapper withRemoveOperationIdPrefixCount(final Integer removeOperationIdPrefixCount) {
this.configurator.addAdditionalProperty("removeOperationIdPrefixCount", removeOperationIdPrefixCount);
return this;
}

public OpenApiClientGeneratorWrapper withModelNamePrefix(final String modelNamePrefix) {
this.configurator.setModelNamePrefix(modelNamePrefix);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -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: org.acme.UserResource.findAll
responses:
"200":
description: OK
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/User"
post:
tags:
- User Resource
description: Add
operationId: org.acme.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: org.acme.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: org.acme.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: org.acme.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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
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

quarkus.openapi-generator.codegen.spec.openapi_prefix_delimiter_yaml.remove-operation-id-prefix=true
quarkus.openapi-generator.codegen.spec.openapi_prefix_delimiter_yaml.remove-operation-id-prefix-delimiter=.

quarkus.openapi-generator.codegen.spec.openapi_prefix_count_yaml.remove-operation-id-prefix=true
quarkus.openapi-generator.codegen.spec.openapi_prefix_count_yaml.remove-operation-id-prefix-delimiter=.
quarkus.openapi-generator.codegen.spec.openapi_prefix_count_yaml.remove-operation-id-prefix-count=3
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,55 @@
@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 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";

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();

}

@Test
void removeOperationIdPrefixDelimiterTest() {
String apiClassName = "org.openapi.quarkus.openapi_prefix_delimiter_yaml.api.UserResourceApi";
String modelClassName = "org.openapi.quarkus.openapi_prefix_delimiter_yaml.model.User";

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();

}

@Test
void apiIsBeingGenerated() {
void removeOperationIdPrefixCountTest() {
String apiClassName = "org.openapi.quarkus.openapi_prefix_count_yaml.api.UserResourceApi";
String modelClassName = "org.openapi.quarkus.openapi_prefix_count_yaml.model.User";

assertThatCode(() -> Class.forName(apiClassName).getMethod("find", Integer.class))
.doesNotThrowAnyException();

Expand Down
14 changes: 14 additions & 0 deletions docs/modules/ROOT/pages/includes/getting-started.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ You can remove operationId prefix (e.g. User_findAll=> findAll). To do that, you
quarkus.openapi-generator.codegen.spec.petstore_json.remove-operation-id-prefix=true
----

Character to use as a delimiter for the prefix. Default is '_'.You can define the prefix delimiter (e.g. User.findAll=> findAll):

[source,properties]
----
quarkus.openapi-generator.codegen.spec.petstore_json.remove-operation-id-prefix-delimiter=.
----

You can define count of delimiter for the prefix (e.g. org.acme.UserResource.findAll=> findAll). Use -1 for last Default:

[source,properties]
----
quarkus.openapi-generator.codegen.spec.petstore_json.remove-operation-id-prefix-count=3
----

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]
Expand Down

0 comments on commit 0719839

Please sign in to comment.