Skip to content

Commit de308b3

Browse files
github-actions[bot]michalsomorahbelmiro
authored
client-headers-factory option (#365) (#373)
* client-header-factory option * client-header-factory fixes * Update README.md * Update README.md * Update README.md * Update README.md * Update integration-tests/security/src/test/java/io/quarkiverse/openapi/generator/it/security/OpenWeatherTest.java * Update integration-tests/security/src/test/java/io/quarkiverse/openapi/generator/it/security/OpenWeatherTest.java * Update integration-tests/security/src/test/java/io/quarkiverse/openapi/generator/it/security/OpenWeatherTest.java * client-header-factory readme section fix * client-header-factory fix loadClass * client-header-factory fix doc * client-header-factory fix format * client-header-factory fix format 2 --------- Co-authored-by: Michal Somora <[email protected]> Co-authored-by: Helber Belmiro <[email protected]>
1 parent 89447cb commit de308b3

File tree

12 files changed

+737
-10
lines changed

12 files changed

+737
-10
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,18 @@ The token propagation can be used with type "oauth2" or "bearer" security scheme
411411
| `quarkus.openapi-generator.[filename].auth.[security_scheme_name].token-propagation=[true,false]` | `quarkus.openapi-generator.petstore_json.auth.petstore_auth.token-propagation=true`<br/>Enables the token propagation for all the operations that are secured with the `petstore_auth` scheme in the `petstore_json` file.
412412
| `quarkus.openapi-generator.[filename].auth.[security_scheme_name].header-name=[http_header_name]` | `quarkus.openapi-generator.petstore_json.auth.petstore_auth.header-name=MyHeaderName`<br/>Says that the authorization token to propagate will be read from the HTTP header `MyHeaderName` instead of the standard HTTP `Authorization` header.
413413

414+
## Headers propagation
415+
Custom headers propagation can be set via MicroProfile configuration `org.eclipse.microprofile.rest.client.propagateHeaders`.
416+
In order to consider this configuration you must force `@RegisterClientHeaders` to use its default MicroProfile `ClientHeadersFactory` implementation. Therefore there is an option `client-headers-factory` where you can set any implementation of `ClientHeadersFactory`.
417+
418+
| Description | Property Key | Example |
419+
| -------------|----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
420+
| Bearer Token | `quarkus.openapi-generator.codegen.spec.[fileName].client-headers-factory` | `quarkus.openapi-generator.codegen.spec.open_weather_yaml.client-headers-factory=org.eclipse.microprofile.rest.client.ext.DefaultClientHeadersFactoryImpl` |
421+
422+
If `client-headers-factory` is set to `none` `@RegisterClientHeaders` will use its default implicit implementation as in the example above.
423+
424+
If no option is set then default generated `AuthenticationPropagationHeadersFactory` class is used.
425+
414426
## Circuit Breaker
415427

416428
You can define the [CircuitBreaker annotation from MicroProfile Fault Tolerance](https://microprofile.io/project/eclipse/microprofile-fault-tolerance/spec/src/main/asciidoc/circuitbreaker.asciidoc)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class CodegenConfig {
3434
private static final String CUSTOM_REGISTER_PROVIDERS_FORMAT = "%s.custom-register-providers";
3535

3636
private static final String RETURN_RESPONSE_PROP_FORMAT = "%s.return-response";
37+
private static final String CLIENT_HEADER_FACTORY_PROP_FORMAT = "%s.client-headers-factory";
3738

3839
/**
3940
* OpenAPI Spec details for codegen configuration.
@@ -113,4 +114,8 @@ public static String getCustomRegisterProvidersFormat(final Path openApiFilePath
113114
public static String getReturnResponsePropertyName(final Path openApiFilePath) {
114115
return String.format(RETURN_RESPONSE_PROP_FORMAT, getBuildTimeSpecPropertyPrefix(openApiFilePath));
115116
}
117+
118+
public static String getClientHeaderFactoryPropertyName(final Path openApiFilePath) {
119+
return String.format(CLIENT_HEADER_FACTORY_PROP_FORMAT, getBuildTimeSpecPropertyPrefix(openApiFilePath));
120+
}
116121
}

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
@@ -59,6 +59,17 @@ public class SpecItemConfig {
5959
@ConfigItem(name = "return-response")
6060
public Optional<Boolean> returnResponse;
6161

62+
/**
63+
* ClientHeaderFactory class that should be used in @RegisterClientHeaders.
64+
* Full class name is expected
65+
* If option is not set then AuthenticationPropagationHeadersFactory is generated and used as value
66+
* for @RegisterClientHeaders
67+
* If set to 'none' then @RegisterClientHeaders will be generated without value and default microprofile implementation will
68+
* be used
69+
*/
70+
@ConfigItem(name = "client-headers-factory")
71+
public Optional<String> clientHeadersFactory;
72+
6273
/**
6374
* Defines the normalizer options.
6475
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.VERBOSE_PROPERTY_NAME;
88
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getAdditionalModelTypeAnnotationsPropertyName;
99
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getBasePackagePropertyName;
10+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getClientHeaderFactoryPropertyName;
1011
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getCustomRegisterProvidersFormat;
1112
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getImportMappingsPropertyName;
1213
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getNormalizerPropertyName;
@@ -163,6 +164,10 @@ protected void generate(final Config config, final Path openApiFilePath, final P
163164
generator.withReturnResponse(config.getOptionalValue(getReturnResponsePropertyName(openApiFilePath), Boolean.class)
164165
.orElse(false));
165166

167+
generator.withClientHeaderFactory(
168+
config.getOptionalValue(getClientHeaderFactoryPropertyName(openApiFilePath), String.class)
169+
.orElse("default"));
170+
166171
SmallRyeConfig smallRyeConfig = config.unwrap(SmallRyeConfig.class);
167172
smallRyeConfig.getOptionalValues(getTypeMappingsPropertyName(openApiFilePath), String.class, String.class)
168173
.ifPresent(generator::withTypeMappings);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ public OpenApiClientGeneratorWrapper withReturnResponse(Boolean returnResponse)
134134
return this;
135135
}
136136

137+
public OpenApiClientGeneratorWrapper withClientHeaderFactory(String clientHeaderFactory) {
138+
configurator.addAdditionalProperty("client-headers-factory", clientHeaderFactory);
139+
return this;
140+
}
141+
137142
public OpenApiClientGeneratorWrapper withImportMappings(final Map<String, String> typeMappings) {
138143
typeMappings.forEach(configurator::addImportMapping);
139144
return this;

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ private void replaceWithQuarkusTemplateFiles() {
5959
new SupportingFile(AUTH_PACKAGE + "/compositeAuthenticationProvider.qute",
6060
authFileFolder(),
6161
"CompositeAuthenticationProvider.java"));
62-
supportingFiles.add(
63-
new SupportingFile("auth/headersFactory.qute",
64-
authFileFolder(),
65-
"AuthenticationPropagationHeadersFactory.java"));
62+
63+
if (this.additionalProperties.get("client-headers-factory") == null ||
64+
this.additionalProperties.get("client-headers-factory").equals("default")) {
65+
supportingFiles.add(
66+
new SupportingFile("auth/headersFactory.qute",
67+
authFileFolder(),
68+
"AuthenticationPropagationHeadersFactory.java"));
69+
}
6670
}
6771

6872
apiTemplateFiles.clear();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
1111
{#if hasAuthMethods}
1212
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
1313
import {package}.auth.CompositeAuthenticationProvider;
14+
{#if client-headers-factory is 'default'}
1415
import {package}.auth.AuthenticationPropagationHeadersFactory;
1516
{/if}
17+
{/if}
1618

1719
import java.io.InputStream;
1820
import java.io.OutputStream;
@@ -38,7 +40,14 @@ import io.quarkiverse.openapi.generator.annotations.GeneratedParam;
3840
@GeneratedClass(value="{openapi:parseUri(inputSpec)}", tag = "{baseName}")
3941
{#if hasAuthMethods}
4042
@RegisterProvider(CompositeAuthenticationProvider.class)
43+
{#when client-headers-factory}
44+
{#is 'default'}
4145
@RegisterClientHeaders(AuthenticationPropagationHeadersFactory.class)
46+
{#is 'none'}
47+
@RegisterClientHeaders
48+
{#else}
49+
@RegisterClientHeaders({client-headers-factory}.class)
50+
{/when}
4251
{/if}
4352
{#for crpClassConfig in custom-register-providers.orEmpty}
4453
@RegisterProvider({crpClassConfig}.class)

0 commit comments

Comments
 (0)