Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rupert/additional function inputs #635

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public enum ConfigName {
GENERATE_PART_FILENAME("generate-part-filename"),
PART_FILENAME_VALUE("part-filename-value"),
USE_FIELD_NAME_IN_PART_FILENAME("use-field-name-in-part-filename"),
ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute");
ADDITIONAL_PROPERTIES_AS_ATTRIBUTE("additional-properties-as-attribute"),
ADDITIONAL_REQUEST_ARGS("additional-request-args");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class CommonItemConfig {
@ConfigItem(name = "additional-api-type-annotations")
public Optional<String> additionalApiTypeAnnotations;

/**
* Add custom/additional HTTP Headers or other args to every request
*/
@ConfigItem(name = "additional-request-args")
public Optional<String> additionalRequestArgs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IHO, I think that the property could be additional-request-headers or something like it. WYT @hbelmiro?

Copy link
Contributor Author

@ru4ert ru4ert Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcruzdev I think your not limited to just headers. See my Test

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!


/**
* Defines if the methods should return {@link jakarta.ws.rs.core.Response} or a model. Default is <code>false</code>.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ protected void generate(final Config config, final Path openApiFilePath, final P
getValues(config, openApiFilePath, CodegenConfig.ConfigName.ADDITIONAL_API_TYPE_ANNOTATIONS, String.class)
.ifPresent(generator::withAdditionalApiTypeAnnotationsConfig);

getValues(config, openApiFilePath, CodegenConfig.ConfigName.ADDITIONAL_REQUEST_ARGS, String.class)
.ifPresent(generator::withAdditionalRequestArgs);

getConfigKeyValue(config, openApiFilePath)
.ifPresentOrElse(generator::withConfigKey,
() -> generator.withConfigKey(getSanitizedFileName(openApiFilePath)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ public OpenApiClientGeneratorWrapper withAdditionalApiTypeAnnotationsConfig(fina
return this;
}

public OpenApiClientGeneratorWrapper withAdditionalRequestArgs(final String additionalRequestArgs) {
if (additionalRequestArgs != null) {
this.configurator.addAdditionalProperty("additionalRequestArgs", additionalRequestArgs.split(";"));
}
return this;
}

public void withTemplateDir(Path templateDir) {
this.configurator.addAdditionalProperty("templateDir", templateDir.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public interface {classname} {
{/if}
{/if}
{/if}
{#if additionalRequestArgs}
{#for arg in additionalRequestArgs}{!
!}{arg}{#if arg_hasNext}, {/if}{/for}{!
!}{#if op.hasFormParams || op.allParams},{/if}
{/if}
{#if op.hasFormParams}
{#if is-resteasy-reactive}
@jakarta.ws.rs.BeanParam {op.operationIdCamelCase}MultipartForm multipartForm{#if op.hasPathParams},{/if}{!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,60 @@ private void verifyApiAdditionalAnnotations(File file) {
}
}

@Test
void verifyAdditionalRequestArgs() throws URISyntaxException {
List<File> generatedFiles = createGeneratorWrapper("petstore-openapi.json")
.withEnabledSecurityGeneration(false)
.withAdditionalRequestArgs(
"@HeaderParam(\"jaxrs-style-header\") String headerValue;@HeaderParam(\"x-correlation-id\") String correlationId;@PathParam(\"stream\") String stream")
.generate("org.additionalHTTPHeaders");
assertFalse(generatedFiles.isEmpty());

generatedFiles.stream()
.filter(file -> file.getPath()
.matches(".*api.*Api.java"))
.forEach(this::verifyApiAdditionalHTTPHeaders);
}

private void verifyApiAdditionalHTTPHeaders(File file) {
try {
CompilationUnit compilationUnit = StaticJavaParser.parse(file);
compilationUnit.findAll(MethodDeclaration.class)
.forEach(c -> {
assertParameter(c.getParameterByName("correlationId"),
"String",
Map.of("HeaderParam",
"\"x-correlation-id\""));
assertParameter(c.getParameterByName("headerValue"),
"String",
Map.of("HeaderParam",
"\"jaxrs-style-header\""));
assertParameter(c.getParameterByName("stream"),
"String",
Map.of("PathParam",
"\"stream\""));
});
} catch (FileNotFoundException e) {
throw new RuntimeException(e.getMessage());
}
}

private void assertParameter(Optional<Parameter> optionalParameter,
String type,
Map<String, String> annotations) {
assertThat(optionalParameter).isPresent();
var parameter = optionalParameter.orElseThrow();
assertThat(parameter.getTypeAsString()).isEqualTo(type);
annotations.forEach((annotationName, annotationValue) -> {
var parameterAnnotation = parameter.getAnnotationByName(annotationName);
assertThat(parameterAnnotation).isPresent();
assertThat(parameterAnnotation.get()
.asSingleMemberAnnotationExpr()
.getMemberValue()
.toString()).hasToString(annotationValue);
});
}

@Test
void verifyAPINormalization() throws Exception {
final List<File> generatedFiles = this.createGeneratorWrapper("open-api-normalizer.json")
Expand Down
17 changes: 17 additions & 0 deletions docs/modules/ROOT/pages/includes/additional-request-args.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
To add custom request specific parameters you can use the `additional-request-args` property.

Should work with:

* @PathParam
* @QueryParam
* @CookieParam
* @FormParam
* @MatrixParam

To use `additional-request-args` as attribute, add the following entry to your properties file. In this example, our spec file is in `src/main/openapi/petstore.json`:

----
quarkus.openapi-generator.codegen.spec.petstore_json.additional-request-args=@CookieParam("cookie") String cookie;@HeaderParam("x-correlation-id") String correlationId
----

This configuration is applied to every generated method.
6 changes: 6 additions & 0 deletions docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ include::./includes/template-customization.adoc[leveloffset=+1, opts=optional]

include::./includes/additional-properties-as-attribute.adoc[leveloffset=+1, opts=optional]

[[additional-request-args]]
== Add Additional Request Arguments to each API Method

include::./includes/additional-request-args.adoc[leveloffset=+1, opts=optional]


== Known Limitations

These are the known limitations of this pre-release version:
Expand Down