Skip to content

Commit 2fb9384

Browse files
Fix #1065 - Add Dynamic URL support (#1094) (#1096)
* Add @url annotation by default on every api.qute generated methods * Fix #1065 - Add Dynamic URL support * Remove debug leftovers --------- Signed-off-by: Ricardo Zanini <[email protected]> Co-authored-by: Ricardo Zanini <[email protected]>
1 parent 3e1e9f3 commit 2fb9384

File tree

11 files changed

+270
-271
lines changed

11 files changed

+270
-271
lines changed

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ enum ConfigName {
7777
GENERATE_MODELS("generate-models"),
7878
BEAN_VALIDATION("use-bean-validation"),
7979
SERIALIZABLE_MODEL("serializable-model"),
80-
EQUALS_HASHCODE("equals-hashcode");
80+
EQUALS_HASHCODE("equals-hashcode"),
81+
USE_DYNAMIC_URL("use-dynamic-url");
8182

8283
private final String name;
8384

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecItemConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Optional;
44

5+
import io.smallrye.config.WithDefault;
56
import io.smallrye.config.WithName;
67

78
/*
@@ -68,4 +69,14 @@ public interface SpecItemConfig extends CommonItemConfig {
6869
@WithName("serializable-model")
6970
Optional<Boolean> serializableModel();
7071

72+
/**
73+
* Whether to enable Dynamic URLs on APIs methods.
74+
* By enabling this property every method on `RestClients` will be annotated with `io.quarkus.rest.client.reactive.Url`.
75+
*
76+
* @see <a href="https://quarkus.io/version/3.20/guides/rest-client#dynamic-base-urls">Dynamic base URLs</a>
77+
*/
78+
@WithName("use-dynamic-url")
79+
@WithDefault("false")
80+
Optional<Boolean> useDynamicUrl();
81+
7182
}

client/deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ protected void generate(OpenApiGeneratorOptions options) {
339339
OpenApiClientGeneratorWrapper.SUPPORTS_ADDITIONAL_PROPERTIES_AS_ATTRIBUTE,
340340
additionalPropertiesAsAttribute.toString());
341341

342+
getValues(smallRyeConfig, openApiFilePath, CodegenConfig.ConfigName.USE_DYNAMIC_URL, Boolean.class)
343+
.ifPresent(generator::withUseDynamicUrl);
344+
342345
generator.generate(basePackage);
343346
}
344347

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private void setDefaults() {
111111
this.configurator.addAdditionalProperty("verbose", FALSE);
112112
this.configurator.addAdditionalProperty(CodegenConstants.SERIALIZABLE_MODEL, FALSE);
113113
this.configurator.addAdditionalProperty("equals-hashcode", TRUE);
114+
this.configurator.addAdditionalProperty("use-dynamic-url", FALSE);
114115
}
115116

116117
/**
@@ -323,6 +324,11 @@ public OpenApiClientGeneratorWrapper withModelNamePrefix(final String modelNameP
323324
return this;
324325
}
325326

327+
public OpenApiClientGeneratorWrapper withUseDynamicUrl(final Boolean useDynamicUrl) {
328+
this.configurator.addAdditionalProperty("use-dynamic-url", useDynamicUrl);
329+
return this;
330+
}
331+
326332
/**
327333
* Main entrypoint, or where to generate the files based on the given base package.
328334
*

client/deployment/src/main/resources/templates/libraries/microprofile/api.qute

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ public interface {classname} {
126126
{#if additionalRequestArgs}
127127
{#for arg in additionalRequestArgs}{!
128128
!}{arg}{#if arg_hasNext}, {/if}{/for}{!
129-
!}{#if op.hasFormParams || op.allParams},{/if}
129+
!}{#if op.allParams || op.hasFormParams},{/if}
130+
{/if}
131+
{#if is-resteasy-reactive && use-dynamic-url}
132+
// See https://quarkus.io/version/3.20/guides/rest-client#dynamic-base-urls
133+
@io.quarkus.rest.client.reactive.Url String dynUrl{#if op.allParams || op.hasFormParams},{/if}
130134
{/if}
131135
{#if op.hasFormParams}
132136
{#if is-resteasy-reactive}

client/deployment/src/test/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapperTest.java

Lines changed: 179 additions & 268 deletions
Large diffs are not rendered by default.

docs/modules/ROOT/pages/client.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ quarkus.openapi-generator.codegen.spec.my_openapi_yaml.serializable-model=true
199199

200200
include::./includes/equals-hashcode.adoc[leveloffset=+1, opts=optional]
201201

202+
[[dynamic-url]]
203+
== Dynamic base URLs
204+
205+
include::./includes/dynamic-url.adoc[leveloffset=+1, opts=optional]
206+
202207
== Known Limitations
203208

204209
=== Supported Arguments
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
If you need the generated `RestClient` to target different server URLs at runtime—rather than relying solely on the static URL from the application configuration—you can enable dynamic base URL support.
3+
4+
To do so, set the following property in your configuration:
5+
6+
[source,properties]
7+
----
8+
quarkus.openapi-generator.codegen.spec.my_openapi_yaml.use-dynamic-url=true
9+
----
10+
11+
When this property is enabled and `quarkus-rest-client` is present on the classpath, the generator will include a method parameter annotated with `@io.quarkus.rest.client.reactive.Url`. This allows your application to supply the target URL dynamically at runtime.
12+
13+
This feature is particularly useful when integrating with multiple instances of the same API or switching endpoints based on contextual information.
14+
15+
For more details, refer to the official Quarkus documentation:
16+
https://quarkus.io/version/3.20/guides/rest-client#dynamic-base-urls

docs/modules/ROOT/pages/includes/quarkus-openapi-generator.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,27 @@ endif::add-copy-button-to-env-var[]
12791279
|boolean
12801280
|
12811281

1282+
a|icon:lock[title=Fixed at build time] [[quarkus-openapi-generator_quarkus-openapi-generator-codegen-spec-spec-item-use-dynamic-url]] [.property-path]##link:#quarkus-openapi-generator_quarkus-openapi-generator-codegen-spec-spec-item-use-dynamic-url[`quarkus.openapi-generator.codegen.spec."spec-item".use-dynamic-url`]##
1283+
ifdef::add-copy-button-to-config-props[]
1284+
config_property_copy_button:+++quarkus.openapi-generator.codegen.spec."spec-item".use-dynamic-url+++[]
1285+
endif::add-copy-button-to-config-props[]
1286+
1287+
1288+
[.description]
1289+
--
1290+
Whether to enable Dynamic URLs on APIs methods. By enabling this property every method on `RestClients` will be annotated with `io.quarkus.rest.client.reactive.Url`.
1291+
1292+
1293+
ifdef::add-copy-button-to-env-var[]
1294+
Environment variable: env_var_with_copy_button:+++QUARKUS_OPENAPI_GENERATOR_CODEGEN_SPEC__SPEC_ITEM__USE_DYNAMIC_URL+++[]
1295+
endif::add-copy-button-to-env-var[]
1296+
ifndef::add-copy-button-to-env-var[]
1297+
Environment variable: `+++QUARKUS_OPENAPI_GENERATOR_CODEGEN_SPEC__SPEC_ITEM__USE_DYNAMIC_URL+++`
1298+
endif::add-copy-button-to-env-var[]
1299+
--
1300+
|boolean
1301+
|`false`
1302+
12821303
a| [[quarkus-openapi-generator_quarkus-openapi-generator-item-configs-auth-auth-configs-token-propagation]] [.property-path]##link:#quarkus-openapi-generator_quarkus-openapi-generator-item-configs-auth-auth-configs-token-propagation[`quarkus.openapi-generator."item-configs".auth."auth-configs".token-propagation`]##
12831304
ifdef::add-copy-button-to-config-props[]
12841305
config_property_copy_button:+++quarkus.openapi-generator."item-configs".auth."auth-configs".token-propagation+++[]

docs/modules/ROOT/pages/includes/quarkus-openapi-generator_quarkus.openapi-generator.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,27 @@ endif::add-copy-button-to-env-var[]
12791279
|boolean
12801280
|
12811281

1282+
a|icon:lock[title=Fixed at build time] [[quarkus-openapi-generator_quarkus-openapi-generator-codegen-spec-spec-item-use-dynamic-url]] [.property-path]##link:#quarkus-openapi-generator_quarkus-openapi-generator-codegen-spec-spec-item-use-dynamic-url[`quarkus.openapi-generator.codegen.spec."spec-item".use-dynamic-url`]##
1283+
ifdef::add-copy-button-to-config-props[]
1284+
config_property_copy_button:+++quarkus.openapi-generator.codegen.spec."spec-item".use-dynamic-url+++[]
1285+
endif::add-copy-button-to-config-props[]
1286+
1287+
1288+
[.description]
1289+
--
1290+
Whether to enable Dynamic URLs on APIs methods. By enabling this property every method on `RestClients` will be annotated with `io.quarkus.rest.client.reactive.Url`.
1291+
1292+
1293+
ifdef::add-copy-button-to-env-var[]
1294+
Environment variable: env_var_with_copy_button:+++QUARKUS_OPENAPI_GENERATOR_CODEGEN_SPEC__SPEC_ITEM__USE_DYNAMIC_URL+++[]
1295+
endif::add-copy-button-to-env-var[]
1296+
ifndef::add-copy-button-to-env-var[]
1297+
Environment variable: `+++QUARKUS_OPENAPI_GENERATOR_CODEGEN_SPEC__SPEC_ITEM__USE_DYNAMIC_URL+++`
1298+
endif::add-copy-button-to-env-var[]
1299+
--
1300+
|boolean
1301+
|`false`
1302+
12821303
a| [[quarkus-openapi-generator_quarkus-openapi-generator-item-configs-auth-auth-configs-token-propagation]] [.property-path]##link:#quarkus-openapi-generator_quarkus-openapi-generator-item-configs-auth-auth-configs-token-propagation[`quarkus.openapi-generator."item-configs".auth."auth-configs".token-propagation`]##
12831304
ifdef::add-copy-button-to-config-props[]
12841305
config_property_copy_button:+++quarkus.openapi-generator."item-configs".auth."auth-configs".token-propagation+++[]

0 commit comments

Comments
 (0)