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

[3.15.x] Backport the fixes for camel-quarkus-rest-openapi codegen #6728

Merged
merged 2 commits into from
Oct 31, 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
12 changes: 12 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ If `true`, use bean validation annotations in the generated model classes.
If `true`, use NON_NULL Jackson annotation in the generated model classes.
| `boolean`
| `false`

|icon:lock[title=Fixed at build time] [[quarkus.camel.openapi.codegen.ignore-unknown-properties]]`link:#quarkus.camel.openapi.codegen.ignore-unknown-properties[quarkus.camel.openapi.codegen.ignore-unknown-properties]`

If `true`, use JsonIgnoreProperties(ignoreUnknown = true) annotation in the generated model classes.
| `boolean`
| `false`

|icon:lock[title=Fixed at build time] [[quarkus.camel.openapi.codegen.additional-properties.-additional-properties]]`link:#quarkus.camel.openapi.codegen.additional-properties.-additional-properties[quarkus.camel.openapi.codegen.additional-properties."additional-properties"]`

Additional properties to be used in the mustache templates.
| `Map<String,String>`
|
|===

[.configuration-legend]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
String models = config.getOptionalValue("quarkus.camel.openapi.codegen.models", String.class).orElse("");
boolean useBeanValidation = config.getValue("quarkus.camel.openapi.codegen.use-bean-validation", Boolean.class);
boolean notNullJackson = config.getValue("quarkus.camel.openapi.codegen.not-null-jackson", Boolean.class);
boolean ignoreUnknownProperties = config.getValue("quarkus.camel.openapi.codegen.ignore-unknown-properties",
Boolean.class);

for (String specFile : specFiles) {
CodegenConfigurator configurator = new CodegenConfigurator();
configurator.setLang("quarkus");
Expand All @@ -95,11 +98,28 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
configurator.getCodegenArguments()
.add(new CodegenArgument().option(CodegenConstants.MODEL_DOCS_OPTION).type("boolean").value("false"));
if (useBeanValidation) {
configurator.getAdditionalProperties().put(USE_BEANVALIDATION, "true");
configurator.getAdditionalProperties().put(USE_BEANVALIDATION, true);
}
if (notNullJackson) {
configurator.getAdditionalProperties().put(NOT_NULL_JACKSON_ANNOTATION, "true");
configurator.getAdditionalProperties().put(NOT_NULL_JACKSON_ANNOTATION, true);
}
if (ignoreUnknownProperties) {
configurator.getAdditionalProperties().put("ignoreUnknownProperties", true);
}
config.getPropertyNames().forEach(name -> {
if (name.startsWith("quarkus.camel.openapi.codegen.additional-properties")) {
String key = name.substring("quarkus.camel.openapi.codegen.additional-properties.".length());
String value = config.getValue(name, String.class);
if (configurator.getAdditionalProperties().containsKey(key)) {
LOG.warn("Overriding existing property: " + key + " with value: " + value);
}
if (value.equals("true") || value.equals("false")) {
configurator.getAdditionalProperties().put(key, Boolean.parseBoolean(value));
} else {
configurator.getAdditionalProperties().put(key, value);
}
}
});

final ClientOptInput input = configurator.toClientOptInput();
new DefaultGenerator().opts(input).generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class QuarkusCodegen extends AbstractJavaCodegen implements BeanValidatio
public QuarkusCodegen() {
super();
importMapping.put("QuarkusRegisterForReflection", "io.quarkus.runtime.annotations.RegisterForReflection");
importMapping.put("JsonIgnoreProperties", "com.fasterxml.jackson.annotation.JsonIgnoreProperties");
supportedLibraries.put("quarkus3", "Quarkus 3 framework");
}

Expand Down Expand Up @@ -100,6 +101,9 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
}
}
model.imports.add("QuarkusRegisterForReflection");
if (additionalProperties.containsKey("ignoreUnknownProperties")) {
model.imports.add("JsonIgnoreProperties");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
limitations under the License.
}}
{{^hideGenerationTimestamp}}@{{#jakarta}}jakarta{{/jakarta}}{{^jakarta}}javax{{/jakarta}}.annotation.Generated(value = "{{generatorClass}}", date = "{{generatedDate}}"){{/hideGenerationTimestamp}}
@RegisterForReflection{{#serializableModel}}(serialization = true){{/serializableModel}}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public enum {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum
return b;
}
}
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + text + "' for '{{{classname}}}' enum.");{{/errorOnUnknownEnum}}
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + input + "' for '{{{classname}}}' enum.");{{/errorOnUnknownEnum}}
}
{{#gson}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
return b;
}
}
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + text + "' for '{{{classname}}}' enum.");{{/errorOnUnknownEnum}}
{{^errorOnUnknownEnum}}return null;{{/errorOnUnknownEnum}}{{#errorOnUnknownEnum}}throw new IllegalArgumentException("Unexpected value '" + input + "' for '{{{classname}}}' enum.");{{/errorOnUnknownEnum}}
}
{{#gson}}
public static class Adapter extends TypeAdapter<{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}> {
Expand All @@ -70,4 +70,4 @@
return {{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue(({{{datatype}}})(value));
}
}{{/gson}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
{{#notNullJacksonAnnotation}}
@JsonInclude(JsonInclude.Include.NON_NULL)
{{/notNullJacksonAnnotation}}
@RegisterForReflection{{#serializableModel}}(serialization = true){{/serializableModel}}
{{#ignoreUnknownProperties}}
@JsonIgnoreProperties(ignoreUnknown = true)
{{/ignoreUnknownProperties}}

public class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{{#parcelableModel}}implements Parcelable {{#serializableModel}}, Serializable {{/serializableModel}}{{#interfaceModels}}{{#@first}}, {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/parcelableModel}}{{^parcelableModel}}{{#serializableModel}}implements Serializable{{#interfaceModels}}{{#@first}}, {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{^serializableModel}}{{#interfaceModels}}{{#@first}}implements {{/@first}}{{classname}}{{^@last}}, {{/@last}}{{#@last}} {{/@last}}{{/interfaceModels}}{{/serializableModel}}{{/parcelableModel}}{
{{#serializableModel}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.quarkus.rest.openapi.runtime;

import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
Expand Down Expand Up @@ -63,6 +64,22 @@ public static class CodeGenConfig {
*/
@ConfigItem(defaultValue = "false")
public boolean notNullJackson;

/**
* If `true`, use JsonIgnoreProperties(ignoreUnknown = true) annotation in the generated model classes.
*
* @asciidoclet
*/
@ConfigItem(defaultValue = "false")
public boolean ignoreUnknownProperties;

/**
* Additional properties to be used in the mustache templates.
*
* @asciidoclet
*/
@ConfigItem
public Map<String, String> additionalProperties;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
quarkus.native.resources.includes=openapi.json,petstore.json,example.yaml
quarkus.camel.openapi.codegen.model-package=org.apache.camel.quarkus.component.rest.openapi.it.model
quarkus.camel.openapi.codegen.not-null-jackson=true
quarkus.camel.openapi.codegen.ignore-unknown-properties=true
quarkus.camel.openapi.codegen.additional-properties.errorOnUnknownEnum=true
camel.rest.bindingMode=json
camel.rest.bindingPackageScan=${quarkus.camel.openapi.codegen.model-package}