This is an automated email from the ASF dual-hosted git repository.
zhfeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
The following commit(s) were added to refs/heads/main by this push:
new 7b4f948bab Introduce additionProperties in rest-openapi codegen and
fix some known issues with mustache template files (#6726)
7b4f948bab is described below
commit 7b4f948bab25d8bff216691448c97046f29ceb9e
Author: Zheng Feng <[email protected]>
AuthorDate: Thu Oct 31 06:50:32 2024 +0800
Introduce additionProperties in rest-openapi codegen and fix some known
issues with mustache template files (#6726)
---
.../pages/reference/extensions/rest-openapi.adoc | 6 ++++++
.../CamelQuarkusSwaggerCodegenProvider.java | 21 ++++++++++++++++++---
.../handlebars/Quarkus/generatedAnnotation.mustache | 2 --
.../resources/handlebars/Quarkus/modelEnum.mustache | 2 +-
.../handlebars/Quarkus/modelInnerEnum.mustache | 4 ++--
.../main/resources/handlebars/Quarkus/pojo.mustache | 4 ++++
.../openapi/runtime/RestOpenApiBuildTimeConfig.java | 9 +++++++++
.../src/main/resources/application.properties | 1 +
8 files changed, 41 insertions(+), 8 deletions(-)
diff --git a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
index 0bad43863d..0fb359d61e 100644
--- a/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
+++ b/docs/modules/ROOT/pages/reference/extensions/rest-openapi.adoc
@@ -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]
diff --git
a/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
b/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
index 23f86e0e4c..546af5455f 100644
---
a/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
+++
b/extensions/rest-openapi/deployment/src/main/java/org/apache/camel/quarkus/component/rest/openapi/deployment/CamelQuarkusSwaggerCodegenProvider.java
@@ -84,6 +84,7 @@ public class CamelQuarkusSwaggerCodegenProvider implements
CodeGenProvider {
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");
@@ -97,14 +98,28 @@ public class CamelQuarkusSwaggerCodegenProvider implements
CodeGenProvider {
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();
diff --git
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/generatedAnnotation.mustache
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/generatedAnnotation.mustache
index 6aa9a052e9..071df2fc88 100644
---
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/generatedAnnotation.mustache
+++
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/generatedAnnotation.mustache
@@ -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}}
diff --git
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelEnum.mustache
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelEnum.mustache
index a86cf7ff4c..9f975db070 100644
---
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelEnum.mustache
+++
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelEnum.mustache
@@ -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}}
diff --git
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelInnerEnum.mustache
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelInnerEnum.mustache
index 9b13af047a..4690242474 100644
---
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelInnerEnum.mustache
+++
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/modelInnerEnum.mustache
@@ -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}}>
{
@@ -70,4 +70,4 @@
return
{{#datatypeWithEnum}}{{{.}}}{{/datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}.fromValue(({{{datatype}}})(value));
}
}{{/gson}}
- }
\ No newline at end of file
+ }
diff --git
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/pojo.mustache
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/pojo.mustache
index fc8b7d72f2..f03c1e3980 100644
---
a/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/pojo.mustache
+++
b/extensions/rest-openapi/deployment/src/main/resources/handlebars/Quarkus/pojo.mustache
@@ -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}}{{/interfaceMode [...]
{{#serializableModel}}
diff --git
a/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
b/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
index 5fce080cdc..891ff14c26 100644
---
a/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
+++
b/extensions/rest-openapi/runtime/src/main/java/org/apache/camel/quarkus/rest/openapi/runtime/RestOpenApiBuildTimeConfig.java
@@ -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;
@@ -85,5 +86,13 @@ public class RestOpenApiBuildTimeConfig {
*/
@ConfigItem(defaultValue = "false")
public boolean ignoreUnknownProperties;
+
+ /**
+ * Additional properties to be used in the mustache templates.
+ *
+ * @asciidoclet
+ */
+ @ConfigItem
+ public Map<String, String> additionalProperties;
}
}
diff --git
a/integration-tests/rest-openapi/src/main/resources/application.properties
b/integration-tests/rest-openapi/src/main/resources/application.properties
index a16f53a6ab..01f7a2133a 100644
--- a/integration-tests/rest-openapi/src/main/resources/application.properties
+++ b/integration-tests/rest-openapi/src/main/resources/application.properties
@@ -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}