Skip to content

Commit 7c170dd

Browse files
Standardize the openapi spec id in configs and add baseUri to the RegisterRestClient (#60)
1 parent 1d7d752 commit 7c170dd

File tree

15 files changed

+57
-37
lines changed

15 files changed

+57
-37
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors-)
55
<!-- ALL-CONTRIBUTORS-BADGE:END -->
66

7+
> **⚠️** This is the instructions for the latest SNAPSHOT version (main branch). Please, see the [latest **released** documentation](https://github.com/quarkiverse/quarkus-openapi-generator/blob/0.5.0/README.md) if you are looking for instructions.
8+
79
Quarkus' extension for generation of [Rest Clients](https://quarkus.io/guides/rest-client) based on OpenAPI specification files.
810

911
This extension is based on the [OpenAPI Generator Tool](https://openapi-generator.tech/). Please consider donation to help them maintain the
@@ -18,7 +20,7 @@ Add the following dependency to your project's `pom.xml` file:
1820
<dependency>
1921
<groupId>io.quarkiverse.openapi.generator</groupId>
2022
<artifactId>quarkus-openapi-generator</artifactId>
21-
<version>0.5.0</version>
23+
<version>1.0.0-SNAPSHOT</version>
2224
</dependency>
2325
```
2426

@@ -50,10 +52,10 @@ Now, create the directory `openapi` under your `src/main/` path and add the Open
5052
To fine tune the configuration for each spec file, add the following entry to your properties file. In this example, our spec file is in `src/main/openapi/petstore.json`:
5153

5254
```properties
53-
quarkus.openapi-generator.codegen.spec."petstore.json".base-package=org.acme.openapi
55+
quarkus.openapi-generator.codegen.spec.petstore_json.base-package=org.acme.openapi
5456
```
5557

56-
Note that the file name is used to configure the specific information for each spec.
58+
Note that the file name is used to configure the specific information for each spec. We follow the [Environment Variables Mapping Rules](https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#environment-variables-mapping-rules) from Microprofile Configuration to sanitize the OpenAPI spec filename. Any non-alphabetic characters are replaced by an underscore `_`.
5759

5860
Run `mvn compile` to generate your classes in `target/generated-sources/open-api-json` path:
5961

@@ -277,7 +279,8 @@ Add the [SmallRye Fault Tolerance extension](https://quarkus.io/guides/smallrye-
277279
Assuming your Open API spec file is in `src/main/openapi/simple-openapi.json`, add the following configuration to your `application.properties` file:
278280

279281
````properties
280-
quarkus.openapi-generator.codegen.spec."simple-openapi.json".base-package=org.acme.openapi.simple
282+
# Note that the file name must have only alphabetic characters or underscores (_).
283+
quarkus.openapi-generator.codegen.spec.simple_openapi_json.base-package=org.acme.openapi.simple
281284
# Enables the CircuitBreaker extension for the byeGet method from the DefaultApi class
282285
org.acme.openapi.simple.api.DefaultApi/byeGet/CircuitBreaker/enabled=true
283286
````
@@ -299,7 +302,7 @@ import javax.ws.rs.core.Response;
299302
import javax.ws.rs.core.MediaType;
300303

301304
@Path("")
302-
@RegisterRestClient
305+
@RegisterRestClient(baseUri="http://localhost/", configKey="simple-openapi_json")
303306
public interface DefaultApi {
304307

305308
@GET
@@ -375,7 +378,7 @@ Then in the client the `org.jboss.resteasy.annotations.providers.multipart.Multi
375378

376379
```java
377380
@Path("/echo")
378-
@RegisterRestClient
381+
@RegisterRestClient(baseUri="http://my.endpoint.com/api/v1", configKey="multipart-requests_yml")
379382
public interface MultipartService {
380383

381384
@POST
@@ -393,7 +396,7 @@ Importantly, if some multipart request bodies contain complex objects (i.e. non-
393396
the `skip-form-model` property corresponding to your spec in the `application.properties` to `false`, e.g.:
394397

395398
```properties
396-
quarkus.openapi-generator.codegen.spec."my-multipart-requests.yml".skip-form-model=false
399+
quarkus.openapi-generator.codegen.spec.my_multipart_requests_yml.skip-form-model=false
397400
```
398401

399402
### Default content-types according to OpenAPI Specification and limitations

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecConfig.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.quarkus.runtime.annotations.ConfigItem;
77
import io.quarkus.runtime.annotations.ConfigPhase;
88
import io.quarkus.runtime.annotations.ConfigRoot;
9+
import io.smallrye.config.common.utils.StringUtil;
910

1011
// This configuration is read in codegen phase (before build time), the annotation is for document purposes and avoiding quarkus warns
1112
@ConfigRoot(name = SpecConfig.BUILD_TIME_CONFIG_PREFIX, phase = ConfigPhase.BUILD_TIME)
@@ -15,7 +16,7 @@ public class SpecConfig {
1516
public static final String API_PKG_SUFFIX = ".api";
1617
public static final String MODEL_PKG_SUFFIX = ".model";
1718
// package visibility for unit tests
18-
static final String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + BUILD_TIME_CONFIG_PREFIX + ".spec.\"%s\"";
19+
static final String BUILD_TIME_SPEC_PREFIX_FORMAT = "quarkus." + BUILD_TIME_CONFIG_PREFIX + ".spec.%s";
1920
private static final String BASE_PACKAGE_PROP_FORMAT = "%s.base-package";
2021
private static final String SKIP_FORM_MODEL_PROP_FORMAT = "%s.skip-form-model";
2122

@@ -44,14 +45,14 @@ public static String getSkipFormModelPropertyName(final Path openApiFilePath) {
4445
/**
4546
* Gets the config prefix for a given OpenAPI file in the path.
4647
* For example, given a path like /home/luke/projects/petstore.json, the returned value is
47-
* `quarkus.openapi-generator."petstore.json"`.
48+
* `quarkus.openapi-generator."petstore_json"`.
49+
* Every the periods (.) in the file name will be replaced by underscore (_).
4850
*/
4951
public static String getBuildTimeSpecPropertyPrefix(final Path openApiFilePath) {
50-
return String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, getEscapedFileName(openApiFilePath));
52+
return String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, getSanitizedFileName(openApiFilePath));
5153
}
5254

53-
private static String getEscapedFileName(final Path openApiFilePath) {
54-
final String uriFilePath = openApiFilePath.toUri().toString();
55-
return uriFilePath.substring(uriFilePath.lastIndexOf("/") + 1);
55+
public static String getSanitizedFileName(final Path openApiFilePath) {
56+
return StringUtil.replaceNonAlphanumericByUnderscores(openApiFilePath.getFileName().toString());
5657
}
5758
}

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.quarkiverse.openapi.generator.deployment.wrapper;
22

3+
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.getSanitizedFileName;
34
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.resolveApiPackage;
45
import static io.quarkiverse.openapi.generator.deployment.SpecConfig.resolveModelPackage;
56
import static java.lang.Boolean.FALSE;
67
import static java.lang.Boolean.TRUE;
78

89
import java.io.File;
910
import java.nio.file.Path;
11+
import java.util.Collections;
1012
import java.util.List;
1113
import java.util.Map;
1214

@@ -51,6 +53,8 @@ public OpenApiClientGeneratorWrapper(final Path specFilePath, final Path outputD
5153
this.configurator = new QuarkusCodegenConfigurator();
5254
this.configurator.setInputSpec(specFilePath.toString());
5355
this.configurator.setOutputDir(outputDir.toString());
56+
this.configurator.addAdditionalProperty("quarkus-generator",
57+
Collections.singletonMap("openApiSpecId", getSanitizedFileName(specFilePath)));
5458
this.generator = new DefaultGenerator();
5559
}
5660

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/QuarkusJavaClientCodegen.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
import org.openapitools.codegen.SupportingFile;
66
import org.openapitools.codegen.languages.JavaClientCodegen;
77
import org.openapitools.codegen.utils.ProcessUtils;
8+
import org.openapitools.codegen.utils.URLPathUtils;
9+
10+
import io.swagger.v3.oas.models.OpenAPI;
811

912
public class QuarkusJavaClientCodegen extends JavaClientCodegen {
1013

1114
private static final String AUTH_PACKAGE = "auth";
15+
/**
16+
* Default server URL (the first one in the OpenAPI spec file servers definition.
17+
*/
18+
private static final String DEFAULT_SERVER_URL = "defaultServerUrl";
1219

1320
public QuarkusJavaClientCodegen() {
1421
// immutable properties
@@ -42,11 +49,11 @@ private void replaceWithQuarkusTemplateFiles() {
4249
ProcessUtils.hasHttpBearerMethods(this.openAPI) ||
4350
ProcessUtils.hasOAuthMethods(this.openAPI)) {
4451
supportingFiles.add(
45-
new SupportingFile("auth/compositeAuthenticationProvider.qute",
52+
new SupportingFile(AUTH_PACKAGE + "/compositeAuthenticationProvider.qute",
4653
authFileFolder(),
4754
"CompositeAuthenticationProvider.java"));
4855
supportingFiles.add(
49-
new SupportingFile("auth/authConfig.qute",
56+
new SupportingFile(AUTH_PACKAGE + "/authConfig.qute",
5057
authFileFolder(),
5158
"AuthConfiguration.java"));
5259
}
@@ -63,4 +70,11 @@ public String authFileFolder() {
6370
// we are only interested in the package path
6471
return apiPackage().replace('.', File.separatorChar) + File.separator + AUTH_PACKAGE;
6572
}
73+
74+
@Override
75+
public void preprocessOpenAPI(OpenAPI openAPI) {
76+
super.preprocessOpenAPI(openAPI);
77+
// add the default server url to the context
78+
additionalProperties.put(DEFAULT_SERVER_URL, URLPathUtils.getServerURL(this.openAPI, serverVariableOverrides()));
79+
}
6680
}

deployment/src/main/resources/templates/api.qute

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import io.quarkiverse.openapi.generator.annotations.GeneratedParam;
2929
*/
3030
{/if}
3131
@Path("{#if useAnnotatedBasePath}{contextPath}{/if}{commonPath}")
32-
@RegisterRestClient
32+
@RegisterRestClient(baseUri="{defaultServerUrl}", configKey="{quarkus-generator.openApiSpecId}")
3333
@GeneratedClass(value="{openapi:parseUri(inputSpec)}", tag = "{baseName}")
3434
{#if hasAuthMethods}
3535
@RegisterProvider(CompositeAuthenticationProvider.class)

deployment/src/test/java/io/quarkiverse/openapi/generator/deployment/SpecConfigTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class SpecConfigTest {
1313
void verifySpaceEncoding() {
1414
final String resolvedPrefix = SpecConfig
1515
.getBuildTimeSpecPropertyPrefix(Path.of("/home/myuser/luke/my test openapi.json"));
16-
assertEquals(resolvedPrefix, String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, "my%20test%20openapi.json"));
16+
assertEquals(resolvedPrefix, String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, "my_test_openapi_json"));
1717
}
1818

1919
@Test
2020
void withSingleFileName() {
2121
final String resolvedPrefix = SpecConfig.getBuildTimeSpecPropertyPrefix(Path.of("my test openapi.json"));
22-
assertEquals(resolvedPrefix, String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, "my%20test%20openapi.json"));
22+
assertEquals(resolvedPrefix, String.format(BUILD_TIME_SPEC_PREFIX_FORMAT, "my_test_openapi_json"));
2323
}
2424

2525
}

deployment/src/test/resources/circuitbreaker/application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# used by io.quarkiverse.openapi.generator.deployment.circuitbreaker.CircuitBreakerConfigurationParserTest
2-
quarkus.openapi-generator.codegen.spec."any-api.json".base-package=org.acme.openapi
3-
quarkus.openapi-generator.codegen.spec."restcountries.json".base-package=org.acme.openapi
2+
quarkus.openapi-generator.codegen.spec.any_api_json.base-package=org.acme.openapi
3+
quarkus.openapi-generator.codegen.spec.restcountries_json.base-package=org.acme.openapi
44

55
org.acme.CountryResource/getCountries/CircuitBreaker/enabled=true
66
org.acme.CountryResource/getCountries/CircuitBreaker/failOn = java.lang.IllegalArgumentException,java.lang.NullPointerException

deployment/src/test/resources/circuitbreaker/circuit_breaker_disabled_application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# used by io.quarkiverse.openapi.generator.deployment.circuitbreaker.CircuitBreakerConfigurationParserTest
2-
quarkus.openapi-generator.codegen.spec."any-api.json".base-package=org.acme.openapi
3-
quarkus.openapi-generator.codegen.spec."restcountries.json".base-package=org.acme.openapi
2+
quarkus.openapi-generator.codegen.spec.any_api_json.base-package=org.acme.openapi
3+
quarkus.openapi-generator.codegen.spec.restcountries_json.base-package=org.acme.openapi
44

55
org.acme.CountryResource/getCountries/CircuitBreaker/enabled=false
66
org.acme.CountryResource/getCountries/CircuitBreaker/failOn = java.lang.IllegalArgumentException,java.lang.NullPointerException

deployment/src/test/resources/circuitbreaker/missing_circuit_breaker_enabled_application.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# used by io.quarkiverse.openapi.generator.deployment.circuitbreaker.CircuitBreakerConfigurationParserTest
2-
quarkus.openapi-generator.codegen.spec."any-api.json".base-package=org.acme.openapi
3-
quarkus.openapi-generator.codegen.spec."restcountries.json".base-package=org.acme.openapi
2+
quarkus.openapi-generator.codegen.spec.any_api_json.base-package=org.acme.openapi
3+
quarkus.openapi-generator.codegen.spec.restcountries_json.base-package=org.acme.openapi
44

55
org.acme.CountryResource/getCountries/CircuitBreaker/failOn = java.lang.IllegalArgumentException,java.lang.NullPointerException
66
org.acme.CountryResource/getCountries/CircuitBreaker/skipOn = java.lang.NumberFormatException, java.lang.IndexOutOfBoundsException

deployment/src/test/resources/codegen/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# Base package for our generated code
3-
quarkus.openapi-generator.codegen.spec."issue-38.yaml".base-package=org.issue38
3+
quarkus.openapi-generator.codegen.spec.issue_38_yaml.base-package=org.issue38
44

55
# Should we generate deprecated attributes for the given model?
66
# By default, deprecated attributes in model classes are generated

0 commit comments

Comments
 (0)