From 411045f85d359375bf40ae4efd0249a9fd74fae1 Mon Sep 17 00:00:00 2001 From: Matheus Cruz Date: Wed, 25 Dec 2024 22:21:02 -0300 Subject: [PATCH] Support dash (-) on RegisterRestClient#configKey --- .../codegen/OpenApiConfigValidator.java | 2 +- .../OpenApiClientGeneratorWrapper.java | 8 +- .../main/openapi/config-key-with-dash.yaml | 274 ++++++++++++++++++ .../src/main/resources/application.properties | 6 +- .../QuarkusConfigKeyWithDashOpenApiTest.java | 31 ++ 5 files changed, 316 insertions(+), 5 deletions(-) create mode 100644 client/integration-tests/config-key/src/main/openapi/config-key-with-dash.yaml create mode 100644 client/integration-tests/config-key/src/test/java/io/quarkiverse/openapi/generator/configkey/QuarkusConfigKeyWithDashOpenApiTest.java diff --git a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiConfigValidator.java b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiConfigValidator.java index 96386230d..e4c55cade 100644 --- a/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiConfigValidator.java +++ b/client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiConfigValidator.java @@ -18,7 +18,7 @@ public final class OpenApiConfigValidator { private static final Logger LOGGER = LoggerFactory.getLogger(OpenApiConfigValidator.class); static final Pattern CONFIG_PATTERN = Pattern.compile( - "quarkus\\.openapi-generator\\.codegen\\.(spec.(?\\w*)\\.)?(?[A-Za-z0-9_\\-]*)\\.?(?.+)?"); + "quarkus\\.openapi-generator\\.codegen\\.(spec.(?[\\w\\-]*)\\.)?(?[A-Za-z0-9_\\-]*)\\.?(?.+)?"); private OpenApiConfigValidator() { } 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 4f06bc514..3ce73326a 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 @@ -21,8 +21,6 @@ import org.openapitools.codegen.DefaultGenerator; import org.openapitools.codegen.config.GlobalSettings; -import io.smallrye.config.common.utils.StringUtil; - /** * Wrapper for the OpenAPIGen tool. * This is the same as calling the Maven plugin or the CLI. @@ -356,11 +354,15 @@ private void consolidatePackageNames() { public void withConfigKey(final String config) { if (config != null && !config.isBlank()) { - this.configurator.addAdditionalProperty("configKey", StringUtil.replaceNonAlphanumericByUnderscores(config)); + this.configurator.addAdditionalProperty("configKey", transformToSafeConfigKey(config)); } } public void withAdditionalPropertiesAsAttribute(final Boolean enable) { this.configurator.addAdditionalProperty("additionalPropertiesAsAttribute", Optional.ofNullable(enable).orElse(FALSE)); } + + public static String transformToSafeConfigKey(String input) { + return input.replaceAll("[^a-zA-Z0-9\\-_]", "_"); + } } diff --git a/client/integration-tests/config-key/src/main/openapi/config-key-with-dash.yaml b/client/integration-tests/config-key/src/main/openapi/config-key-with-dash.yaml new file mode 100644 index 000000000..4a853fe0b --- /dev/null +++ b/client/integration-tests/config-key/src/main/openapi/config-key-with-dash.yaml @@ -0,0 +1,274 @@ +--- +openapi: 3.0.3 +info: + title: greeting-flow API + version: "1.0" +paths: + /: + post: + requestBody: + content: + '*/*': + schema: + $ref: '#/components/schemas/CloudEvent' + responses: + "200": + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Response' + /hello: + get: + tags: + - Reactive Greeting Resource + operationId: hello + responses: + "200": + description: OK + content: + text/plain: + schema: + type: string + /messaging/topics: + get: + tags: + - Quarkus Topics Information Resource + responses: + "200": + description: OK +components: + schemas: + CloudEvent: + type: object + properties: + specVersion: + $ref: '#/components/schemas/SpecVersion' + id: + type: string + type: + type: string + source: + format: uri + type: string + dataContentType: + type: string + dataSchema: + format: uri + type: string + subject: + type: string + time: + format: date-time + type: string + attributeNames: + uniqueItems: true + type: array + items: + type: string + extensionNames: + uniqueItems: true + type: array + items: + type: string + data: + $ref: '#/components/schemas/CloudEventData' + CloudEventData: + type: object + EntityTag: + type: object + properties: + value: + type: string + weak: + type: boolean + Family: + enum: + - INFORMATIONAL + - SUCCESSFUL + - REDIRECTION + - CLIENT_ERROR + - SERVER_ERROR + - OTHER + type: string + Link: + type: object + properties: + uri: + format: uri + type: string + uriBuilder: + $ref: '#/components/schemas/UriBuilder' + rel: + type: string + rels: + type: array + items: + type: string + title: + type: string + type: + type: string + params: + type: object + additionalProperties: + type: string + Locale: + type: object + properties: + language: + type: string + script: + type: string + country: + type: string + variant: + type: string + extensionKeys: + uniqueItems: true + type: array + items: + format: byte + type: string + unicodeLocaleAttributes: + uniqueItems: true + type: array + items: + type: string + unicodeLocaleKeys: + uniqueItems: true + type: array + items: + type: string + iSO3Language: + type: string + iSO3Country: + type: string + displayLanguage: + type: string + displayScript: + type: string + displayCountry: + type: string + displayVariant: + type: string + displayName: + type: string + MediaType: + type: object + properties: + type: + type: string + subtype: + type: string + parameters: + type: object + additionalProperties: + type: string + wildcardType: + type: boolean + wildcardSubtype: + type: boolean + MultivaluedMapStringObject: + type: object + additionalProperties: + type: array + items: + type: object + MultivaluedMapStringString: + type: object + additionalProperties: + type: array + items: + type: string + NewCookie: + type: object + properties: + name: + type: string + value: + type: string + version: + format: int32 + type: integer + path: + type: string + domain: + type: string + comment: + type: string + maxAge: + format: int32 + type: integer + expiry: + format: date + type: string + secure: + type: boolean + httpOnly: + type: boolean + Response: + type: object + properties: + status: + format: int32 + type: integer + statusInfo: + $ref: '#/components/schemas/StatusType' + entity: + type: object + mediaType: + $ref: '#/components/schemas/MediaType' + language: + $ref: '#/components/schemas/Locale' + length: + format: int32 + type: integer + allowedMethods: + uniqueItems: true + type: array + items: + type: string + cookies: + type: object + additionalProperties: + $ref: '#/components/schemas/NewCookie' + entityTag: + $ref: '#/components/schemas/EntityTag' + date: + format: date + type: string + lastModified: + format: date + type: string + location: + format: uri + type: string + links: + uniqueItems: true + type: array + items: + $ref: '#/components/schemas/Link' + metadata: + $ref: '#/components/schemas/MultivaluedMapStringObject' + headers: + $ref: '#/components/schemas/MultivaluedMapStringObject' + stringHeaders: + $ref: '#/components/schemas/MultivaluedMapStringString' + SpecVersion: + enum: + - V03 + - V1 + type: string + StatusType: + type: object + properties: + statusCode: + format: int32 + type: integer + family: + $ref: '#/components/schemas/Family' + reasonPhrase: + type: string + UriBuilder: + type: object diff --git a/client/integration-tests/config-key/src/main/resources/application.properties b/client/integration-tests/config-key/src/main/resources/application.properties index b0bfd21e4..8db83ec60 100644 --- a/client/integration-tests/config-key/src/main/resources/application.properties +++ b/client/integration-tests/config-key/src/main/resources/application.properties @@ -15,4 +15,8 @@ quarkus.openapi-generator.codegen.spec.empty_config_key_yaml.config-key= quarkus.openapi-generator.codegen.spec.empty_config_key_yaml.additional-api-type-annotations=@io.quarkiverse.openapi.generator.configkey.CustomAnnotation quarkus.rest-client.empty_config_key_yaml.url=http://localhost:8080 -quarkus.keycloak.devservices.enabled=false \ No newline at end of file +quarkus.keycloak.devservices.enabled=false + +# config-key with `-` +quarkus.openapi-generator.codegen.spec.config_key_with_dash_yaml.config-key=my-api +quarkus.rest-client.my-api.url=http://localhost:8080 diff --git a/client/integration-tests/config-key/src/test/java/io/quarkiverse/openapi/generator/configkey/QuarkusConfigKeyWithDashOpenApiTest.java b/client/integration-tests/config-key/src/test/java/io/quarkiverse/openapi/generator/configkey/QuarkusConfigKeyWithDashOpenApiTest.java new file mode 100644 index 000000000..4efa7e4e9 --- /dev/null +++ b/client/integration-tests/config-key/src/test/java/io/quarkiverse/openapi/generator/configkey/QuarkusConfigKeyWithDashOpenApiTest.java @@ -0,0 +1,31 @@ +package io.quarkiverse.openapi.generator.configkey; + +import static org.assertj.core.api.Assertions.assertThat; + +import jakarta.inject.Inject; + +import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; +import org.eclipse.microprofile.rest.client.inject.RestClient; +import org.junit.jupiter.api.Test; +import org.openapi.quarkus.config_key_with_dash_yaml.api.DefaultApi; + +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +class QuarkusConfigKeyWithDashOpenApiTest { + + @RestClient + @Inject + DefaultApi defaultApi; + + @Test + void apiIsBeingGenerated() { + assertThat(defaultApi).isNotNull(); + } + + @Test + void config_key_should_have_dash() { + assertThat(DefaultApi.class.getAnnotation(RegisterRestClient.class)).isNotNull(); + assertThat(DefaultApi.class.getAnnotation(RegisterRestClient.class).configKey()).isEqualTo("my-api"); + } +}