gnodet-bot commented on code in PR #30633:
URL: https://github.com/apache/camel/pull/30633#discussion_r4661111792
##########
components/camel-openapi-java/src/main/docs/openapi-java.adoc:
##########
@@ -186,3 +186,28 @@ restConfiguration().component("netty-http")
.host("localhost")
.port(8080);
----
+
+== Custom model converters
+
+When Camel generates an OpenAPI document from REST DSL model classes, it looks
in the Camel Registry for a bean
+implementing `org.apache.camel.openapi.RestModelConverters`. When exactly one
such bean is registered, Camel uses
Review Comment:
⚠️ **Missing behavior: more than one bean registered.**
The paragraph documents the single-bean and no-bean cases but is silent
about what happens when more than one `RestModelConverters` bean is registered
in the registry.
`CamelContextHelper.findSingleByType` returns `null` when multiple beans are
found, so Camel silently falls back to `DefaultRestModelConverters`. This
silent fallback could confuse users who register two custom converters
expecting both to be used. Please add a sentence explaining this case, e.g.:
> When more than one such bean is registered, Camel logs a warning and falls
back to `DefaultRestModelConverters`.
(or document the actual behavior — if no warning is logged, that's worth
noting too.)
##########
components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderModelTest.java:
##########
@@ -127,4 +128,22 @@ public void testReaderReadV3(String version) throws
Exception {
context.stop();
}
+
+ @Test
+ public void testCustomRestModelConvertersFromRegistry() throws Exception {
+ DefaultRestModelConverters converters = new
DefaultRestModelConverters();
+ converters.addClassToSkip(User.class.getName());
+ context.getRegistry().bind("restModelConverters", converters);
+
+ BeanConfig config = new BeanConfig();
+ config.setVersion("3.0");
+ RestOpenApiReader reader = new RestOpenApiReader();
+
+ OpenAPI openApi = reader.read(context, context.getRestDefinitions(),
config, context.getName(),
+ new DefaultClassResolver());
+
+ assertNotNull(openApi);
+ assertFalse(openApi.getComponents() != null &&
openApi.getComponents().getSchemas() != null
+ && openApi.getComponents().getSchemas().containsKey("User"));
Review Comment:
⚠️ **Weak assertion — passes vacuously when schema generation fails.**
`assertFalse(A && B && C)` evaluates to `assertFalse(false)` (always passes)
when `getComponents()` returns `null`. If schema generation is broken and
produces no components at all, the test still passes.
Consider adding an assertion that at least one expected schema _is_ present
before checking that `User` is absent:
```java
assertNotNull(openApi.getComponents(), "Components should be generated");
assertNotNull(openApi.getComponents().getSchemas(), "Schemas should be
generated");
// User should be excluded
assertFalse(openApi.getComponents().getSchemas().containsKey("User"));
```
##########
components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestModelConverters.java:
##########
@@ -16,130 +16,44 @@
*/
package org.apache.camel.openapi;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import com.fasterxml.jackson.databind.JavaType;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import io.swagger.v3.core.converter.AnnotatedType;
import io.swagger.v3.core.converter.ModelConverter;
-import io.swagger.v3.core.converter.ModelConverterContext;
-import io.swagger.v3.core.converter.ModelConverters;
-import io.swagger.v3.core.jackson.ModelResolver;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
/**
- * A Camel extended {@link ModelConverters} where we are appending vendor
extensions to include the java class name of
- * the model classes.
+ * Converts Java model classes to OpenAPI schemas used by the REST DSL OpenAPI
document generator.
*/
-@SuppressWarnings("rawtypes")
-public class RestModelConverters {
-
- private static final ModelConverters MODEL31_CONVERTERS;
-
- static {
- MODEL31_CONVERTERS = new ModelConverters(true);
- MODEL31_CONVERTERS.addConverter(new
ClassNameExtensionModelResolver(new FqnModelResolver(true)));
- }
-
- private static final ModelConverters MODEL30_CONVERTERS;
-
- static {
- MODEL30_CONVERTERS = new ModelConverters();
- MODEL30_CONVERTERS.addConverter(new
ClassNameExtensionModelResolver(new FqnModelResolver()));
- }
-
- private final boolean openapi31;
-
- public RestModelConverters(boolean openapi31) {
- this.openapi31 = openapi31;
- }
-
- public List<? extends Schema<?>> readClass(OpenAPI oasDocument, Class<?>
clazz) {
- if (clazz.equals(File.class)) {
- // File is a special type in OAS2 / OAS3 (no model)
- return null;
- } else {
- return readClassOpenApi3(clazz);
- }
- }
-
- private List<? extends Schema<?>> readClassOpenApi3(Class<?> clazz) {
- String name = clazz.getName();
- if (!name.contains(".")) {
- return null;
- }
-
- ModelConverters modelConverters = openapi31 ? MODEL31_CONVERTERS :
MODEL30_CONVERTERS;
- Map<String, Schema> swaggerModel = modelConverters.readAll(clazz);
- List<Schema<?>> modelSchemas = new ArrayList<>();
- swaggerModel.forEach((key, schema) -> {
- schema.setName(key);
- modelSchemas.add(schema);
- });
- return modelSchemas;
- }
-
- private static class FqnModelResolver extends ModelResolver {
- public FqnModelResolver() {
- this(false);
- }
-
- public FqnModelResolver(boolean openapi31) {
- this(new ObjectMapper());
- openapi31(openapi31);
- }
-
- public FqnModelResolver(ObjectMapper mapper) {
- super(mapper);
- this._typeNameResolver.setUseFqn(true);
- }
- }
-
- private static class ClassNameExtensionModelResolver extends ModelResolver
{
- private final ModelResolver delegate;
-
- public ClassNameExtensionModelResolver() {
- this(new ModelResolver(new ObjectMapper()));
- ModelResolver.composedModelPropertiesAsSibling = true;
- }
-
- public ClassNameExtensionModelResolver(ModelResolver delegate) {
- super(delegate.objectMapper());
- this.delegate = delegate;
- }
-
- @Override
- public Schema resolve(AnnotatedType annotatedType,
ModelConverterContext context, Iterator<ModelConverter> next) {
- Schema<?> result = delegate.resolve(annotatedType, context, next);
- if (result != null) {
- String rt = result.getType();
- if (rt == null && result.getTypes() != null) {
- rt = result.getTypes().size() == 1 ?
result.getTypes().iterator().next() : null;
- }
- if (Objects.equals("object", rt)) {
- JavaType type;
- if (annotatedType.getType() instanceof JavaType) {
- type = (JavaType) annotatedType.getType();
- } else {
- type = _mapper.constructType(annotatedType.getType());
- }
-
- if (!type.isContainerType()) {
- Map<String, String> value = new HashMap<>();
- value.put("type", "string");
- value.put("format", type.getRawClass().getName());
- result.addExtension("x-className", value);
- }
- }
- }
- return result;
- }
- }
+public interface RestModelConverters {
Review Comment:
🚨 **Breaking API change — upgrade guide entry missing.**
Changing `RestModelConverters` from a concrete `public class` (with
`RestModelConverters(boolean openapi31)` constructor) to a `public interface`
is a **binary-incompatible change**. Any downstream code that instantiates it
via `new RestModelConverters(true)` or subclasses it will break.
Please add an entry to `camel-4x-upgrade-guide-4_24.adoc` documenting:
1. That `RestModelConverters` is now an interface
2. The migration path: replace `new RestModelConverters(openapi31)` with
`new DefaultRestModelConverters()`
##########
components/camel-openapi-java/src/main/java/org/apache/camel/openapi/DefaultRestModelConverters.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.openapi;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.swagger.v3.core.converter.AnnotatedType;
+import io.swagger.v3.core.converter.ModelConverter;
+import io.swagger.v3.core.converter.ModelConverterContext;
+import io.swagger.v3.core.converter.ModelConverters;
+import io.swagger.v3.core.jackson.ModelResolver;
+import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.SpecVersion;
+import io.swagger.v3.oas.models.media.Schema;
+
+/**
+ * Default {@link RestModelConverters} implementation backed by Swagger's
model converters.
Review Comment:
⚠️ **Missing `@since` tag.**
This is a new public API class. Please add a `@since` Javadoc tag indicating
the Camel version that introduced it:
```suggestion
/**
* Default {@link RestModelConverters} implementation backed by Swagger's
model converters.
*
* @since 4.X.0
*/
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]