Skip to content

Commit 39ebfb9

Browse files
author
jomin mathew
committed
Fixes #6169. Add OpenAPI Validator extension
Add a Camel Quarkus extension for the camel-openapi-validator component, providing native image support for JSON Schema validation of REST API requests against OpenAPI specifications. The extension registers reflective classes for keyword validators, native image resources for JSON Schema draft directories, and runtime-initialized classes required by the underlying Atlassian swagger-request-validator library. The integration test uses the REST DSL consumer with clientRequestValidation enabled to exercise the full validation path, including JSON Schema keyword validation for required fields and type checking.
1 parent 909b1ac commit 39ebfb9

23 files changed

Lines changed: 856 additions & 0 deletions

File tree

catalog/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,6 +3165,19 @@
31653165
</exclusion>
31663166
</exclusions>
31673167
</dependency>
3168+
<dependency>
3169+
<groupId>org.apache.camel.quarkus</groupId>
3170+
<artifactId>camel-quarkus-openapi-validator</artifactId>
3171+
<version>${project.version}</version>
3172+
<type>pom</type>
3173+
<scope>test</scope>
3174+
<exclusions>
3175+
<exclusion>
3176+
<groupId>*</groupId>
3177+
<artifactId>*</artifactId>
3178+
</exclusion>
3179+
</exclusions>
3180+
</dependency>
31683181
<dependency>
31693182
<groupId>org.apache.camel.quarkus</groupId>
31703183
<artifactId>camel-quarkus-opensearch</artifactId>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Do not edit directly!
2+
# This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
3+
cqArtifactId: camel-quarkus-openapi-validator
4+
cqArtifactIdBase: openapi-validator
5+
cqNativeSupported: true
6+
cqStatus: Stable
7+
cqDeprecated: false
8+
cqJvmSince: 3.38.0
9+
cqNativeSince: 3.38.0
10+
cqCamelPartName: openapi-validator
11+
cqCamelPartTitle: Openapi Validator
12+
cqCamelPartDescription: OpenAPI validator for Camel Rest DSL
13+
cqExtensionPageTitle: OpenAPI Validator

docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
*** xref:reference/extensions/once.adoc[Once]
258258
*** xref:reference/extensions/openai.adoc[OpenAI]
259259
*** xref:reference/extensions/openapi-java.adoc[OpenAPI Java]
260+
*** xref:reference/extensions/openapi-validator.adoc[OpenAPI Validator]
260261
*** xref:reference/extensions/opensearch.adoc[OpenSearch]
261262
*** xref:reference/extensions/openstack.adoc[OpenStack]
262263
*** xref:reference/extensions/opentelemetry.adoc[OpenTelemetry]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Do not edit directly!
2+
// This file was generated by camel-quarkus-maven-plugin:update-extension-doc-page
3+
[id="extensions-openapi-validator"]
4+
= OpenAPI Validator
5+
:linkattrs:
6+
:cq-artifact-id: camel-quarkus-openapi-validator
7+
:cq-native-supported: true
8+
:cq-status: Stable
9+
:cq-status-deprecation: Stable
10+
:cq-description: OpenAPI validator for Camel Rest DSL
11+
:cq-deprecated: false
12+
:cq-jvm-since: 3.38.0
13+
:cq-native-since: 3.38.0
14+
15+
ifeval::[{doc-show-badges} == true]
16+
[.badges]
17+
[.badge-key]##JVM since##[.badge-supported]##3.38.0## [.badge-key]##Native since##[.badge-supported]##3.38.0##
18+
endif::[]
19+
20+
OpenAPI validator for Camel Rest DSL
21+
22+
[id="extensions-openapi-validator-whats-inside"]
23+
== What's inside
24+
25+
* xref:{cq-camel-components}:others:openapi-validator.adoc[Openapi Validator]
26+
27+
Please refer to the above link for usage and configuration details.
28+
29+
[id="extensions-openapi-validator-maven-coordinates"]
30+
== Maven coordinates
31+
32+
https://{link-quarkus-code-generator}/?extension-search=camel-quarkus-openapi-validator[Create a new project with this extension on {link-quarkus-code-generator}, window="_blank"]
33+
34+
Or add the coordinates to your existing project:
35+
36+
[source,xml]
37+
----
38+
<dependency>
39+
<groupId>org.apache.camel.quarkus</groupId>
40+
<artifactId>camel-quarkus-openapi-validator</artifactId>
41+
</dependency>
42+
----
43+
ifeval::[{doc-show-user-guide-link} == true]
44+
Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.
45+
endif::[]
46+
47+
[id="extensions-openapi-validator-usage"]
48+
== Usage
49+
[id="extensions-openapi-validator-usage-native-mode"]
50+
=== Native Mode
51+
52+
When using a classpath-based OpenAPI specification in native mode, the file must be explicitly included in the native image. Add the following to your `application.properties`:
53+
54+
[source,properties]
55+
----
56+
quarkus.native.resources.includes=openapi.json
57+
----
58+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<parent>
25+
<groupId>org.apache.camel.quarkus</groupId>
26+
<artifactId>camel-quarkus-openapi-validator-parent</artifactId>
27+
<version>3.38.0-SNAPSHOT</version>
28+
<relativePath>../pom.xml</relativePath>
29+
</parent>
30+
31+
<artifactId>camel-quarkus-openapi-validator-deployment</artifactId>
32+
<name>Camel Quarkus :: OpenAPI Validator :: Deployment</name>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.apache.camel.quarkus</groupId>
37+
<artifactId>camel-quarkus-rest-openapi-deployment</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.apache.camel.quarkus</groupId>
41+
<artifactId>camel-quarkus-openapi-validator</artifactId>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<configuration>
51+
<annotationProcessorPaths>
52+
<path>
53+
<groupId>io.quarkus</groupId>
54+
<artifactId>quarkus-extension-processor</artifactId>
55+
<version>${quarkus.version}</version>
56+
</path>
57+
</annotationProcessorPaths>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.quarkus.component.openapi.validator.deployment;
18+
19+
import com.atlassian.oai.validator.schema.SwaggerV20Library;
20+
import com.github.fge.jackson.JsonLoader;
21+
import com.github.fge.jackson.JsonNodeReader;
22+
import com.github.fge.jsonschema.core.messages.JsonSchemaCoreMessageBundle;
23+
import com.github.fge.jsonschema.core.messages.JsonSchemaSyntaxMessageBundle;
24+
import com.github.fge.jsonschema.core.util.RegexECMA262Helper;
25+
import com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator;
26+
import com.github.fge.jsonschema.messages.JsonSchemaConfigurationBundle;
27+
import com.github.fge.jsonschema.messages.JsonSchemaValidationBundle;
28+
import io.quarkus.deployment.annotations.BuildProducer;
29+
import io.quarkus.deployment.annotations.BuildStep;
30+
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
31+
import io.quarkus.deployment.builditem.FeatureBuildItem;
32+
import io.quarkus.deployment.builditem.IndexDependencyBuildItem;
33+
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
34+
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceDirectoryBuildItem;
35+
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
36+
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
37+
import org.jboss.jandex.ClassInfo;
38+
import org.jboss.jandex.DotName;
39+
40+
class OpenapiValidatorProcessor {
41+
42+
private static final String FEATURE = "camel-openapi-validator";
43+
private static final DotName ABSTRACT_KEYWORD_VALIDATOR = DotName
44+
.createSimple(AbstractKeywordValidator.class.getName());
45+
46+
@BuildStep
47+
FeatureBuildItem feature() {
48+
return new FeatureBuildItem(FEATURE);
49+
}
50+
51+
@BuildStep
52+
void indexDependencies(BuildProducer<IndexDependencyBuildItem> indexDependency) {
53+
indexDependency.produce(new IndexDependencyBuildItem("com.atlassian.oai", "swagger-request-validator-core"));
54+
indexDependency.produce(new IndexDependencyBuildItem("com.github.java-json-tools", "json-schema-validator"));
55+
indexDependency.produce(new IndexDependencyBuildItem("com.github.java-json-tools", "json-schema-core"));
56+
}
57+
58+
@BuildStep
59+
void registerReflectiveClasses(
60+
CombinedIndexBuildItem combinedIndex,
61+
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses) {
62+
63+
// Keyword validators are instantiated via ReflectionKeywordValidatorFactory using Constructor.newInstance()
64+
String[] keywordValidators = combinedIndex.getIndex()
65+
.getAllKnownSubclasses(ABSTRACT_KEYWORD_VALIDATOR)
66+
.stream()
67+
.map(ClassInfo::name)
68+
.map(DotName::toString)
69+
.toArray(String[]::new);
70+
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(keywordValidators).constructors(true).build());
71+
72+
// Message bundles are instantiated via MessageBundles.getBundle() using Constructor.newInstance()
73+
reflectiveClasses.produce(ReflectiveClassBuildItem.builder(
74+
SwaggerV20Library.ValidationBundle.class.getName(),
75+
SwaggerV20Library.SyntaxBundle.class.getName(),
76+
JsonSchemaCoreMessageBundle.class.getName(),
77+
JsonSchemaSyntaxMessageBundle.class.getName(),
78+
JsonSchemaConfigurationBundle.class.getName(),
79+
JsonSchemaValidationBundle.class.getName()).constructors(true).build());
80+
}
81+
82+
@BuildStep
83+
void runtimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> runtimeInitializedClasses) {
84+
runtimeInitializedClasses
85+
.produce(new RuntimeInitializedClassBuildItem(JsonNodeReader.class.getName()));
86+
runtimeInitializedClasses
87+
.produce(new RuntimeInitializedClassBuildItem(JsonLoader.class.getName()));
88+
runtimeInitializedClasses
89+
.produce(new RuntimeInitializedClassBuildItem(RegexECMA262Helper.class.getName()));
90+
}
91+
92+
@BuildStep
93+
void registerNativeImageResources(
94+
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
95+
BuildProducer<NativeImageResourceDirectoryBuildItem> nativeImageResourceDirectories) {
96+
nativeImageResources.produce(new NativeImageResourceBuildItem(
97+
"swagger/validation/default-levels.properties",
98+
"swagger/validation/schema-validation.properties",
99+
"swagger/validation/messages.properties",
100+
"com/github/fge/jsonschema/validator/validation.properties",
101+
"com/github/fge/jsonschema/validator/configuration.properties",
102+
"com/github/fge/jsonschema/core/core.properties",
103+
"com/github/fge/jsonschema/core/syntax.properties",
104+
"com/github/fge/jackson/jsonNodeReader.properties",
105+
"com/github/fge/jackson/jsonpointer.properties",
106+
"com/github/fge/uritemplate/messages.properties"));
107+
// Register entire draft schema directories to capture all schema files (schema, hyper-schema, links, etc.)
108+
nativeImageResourceDirectories.produce(new NativeImageResourceDirectoryBuildItem("draftv3"));
109+
nativeImageResourceDirectories.produce(new NativeImageResourceDirectoryBuildItem("draftv4"));
110+
}
111+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one or more
5+
contributor license agreements. See the NOTICE file distributed with
6+
this work for additional information regarding copyright ownership.
7+
The ASF licenses this file to You under the Apache License, Version 2.0
8+
(the "License"); you may not use this file except in compliance with
9+
the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.0.0"
21+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23+
<modelVersion>4.0.0</modelVersion>
24+
<parent>
25+
<groupId>org.apache.camel.quarkus</groupId>
26+
<artifactId>camel-quarkus-extensions</artifactId>
27+
<version>3.38.0-SNAPSHOT</version>
28+
<relativePath>../pom.xml</relativePath>
29+
</parent>
30+
31+
<artifactId>camel-quarkus-openapi-validator-parent</artifactId>
32+
<name>Camel Quarkus :: OpenAPI Validator</name>
33+
<packaging>pom</packaging>
34+
35+
<modules>
36+
<module>deployment</module>
37+
<module>runtime</module>
38+
</modules>
39+
</project>

0 commit comments

Comments
 (0)