Skip to content

Commit

Permalink
Introduce additionProperties in rest-openapi codegen and fix some kno…
Browse files Browse the repository at this point in the history
…wn issues with mustache template files (#6726)
  • Loading branch information
zhfeng authored Oct 30, 2024
1 parent 7a84cff commit 7b4f948
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ If `true`, use NON_NULL Jackson annotation in the generated model classes.
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 @@ -84,6 +84,7 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
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 @@ -97,14 +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");
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 @@ -15,5 +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}}
{{#ignoreUnknownProperties}}@JsonIgnoreProperties(ignoreUnknown = true){{/ignoreUnknownProperties}}
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 @@ -85,5 +86,13 @@ public static class CodeGenConfig {
*/
@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 @@ -18,5 +18,6 @@ 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}

0 comments on commit 7b4f948

Please sign in to comment.