This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new aee0bf938875 CAMEL-25510: camel-openapi-java - put the allowable 
values of an array parameter on its items
aee0bf938875 is described below

commit aee0bf9388750a2092b3a0a14be22144d546223f
Author: allthingssecurity <[email protected]>
AuthorDate: Sat Oct 10 12:40:38 2026 +0530

    CAMEL-25510: camel-openapi-java - put the allowable values of an array 
parameter on its items
    
    For a Rest DSL parameter with dataType("array") and allowableValues(...),
    RestOpenApiReader put the values in the enum of the array schema itself.
    In OpenAPI 3 an enum constrains the whole value, so the parameter
    accepted no array at all: validators built from the document rejected
    every request with it, and generators produced a wrong type.
    
    defineSchemas now creates the items schema first and sets the enum,
    converted to the item type as before, on items. Non-array parameters and
    arrays without allowable values generate the same schema as before.
    
    Closes #27643
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../apache/camel/openapi/RestOpenApiReader.java    |  17 +--
 .../RestOpenApiReaderArrayAllowableValuesTest.java | 126 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_23.adoc    |   7 ++
 3 files changed, 143 insertions(+), 7 deletions(-)

diff --git 
a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
 
b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
index 2f05c6f6d303..9176e41adac4 100644
--- 
a/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
+++ 
b/components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java
@@ -814,13 +814,6 @@ public class RestOpenApiReader {
             final List<String> allowableValues,
             final Class<?> type) {
         Schema parameterSchema = serializableParameter.getSchema();
-        if (allowableValues != null && !allowableValues.isEmpty()) {
-            if (String.class.equals(type)) {
-                parameterSchema.setEnum(allowableValues);
-            } else {
-                convertAndSetItemsEnum(parameterSchema, allowableValues, type);
-            }
-        }
         if (Objects.equals(parameterSchema.getType(), "array")) {
 
             Schema<?> itemsSchema;
@@ -851,6 +844,16 @@ public class RestOpenApiReader {
 
             parameterSchema.setItems(itemsSchema);
         }
+        if (allowableValues != null && !allowableValues.isEmpty()) {
+            // the allowable values of an array parameter restrict its items 
(an enum on the array schema itself
+            // would only accept arrays equal to one of the values, that is, 
no array at all)
+            Schema enumSchema = parameterSchema.getItems() != null ? 
parameterSchema.getItems() : parameterSchema;
+            if (String.class.equals(type)) {
+                enumSchema.setEnum(allowableValues);
+            } else {
+                convertAndSetItemsEnum(enumSchema, allowableValues, type);
+            }
+        }
     }
 
     private static void convertAndSetItemsEnum(
diff --git 
a/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderArrayAllowableValuesTest.java
 
b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderArrayAllowableValuesTest.java
new file mode 100644
index 000000000000..cef0db8657b8
--- /dev/null
+++ 
b/components/camel-openapi-java/src/test/java/org/apache/camel/openapi/RestOpenApiReaderArrayAllowableValuesTest.java
@@ -0,0 +1,126 @@
+/*
+ * 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.util.List;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.media.Schema;
+import io.swagger.v3.oas.models.parameters.Parameter;
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.engine.DefaultClassResolver;
+import org.apache.camel.model.rest.RestParamType;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * The allowable values of an array parameter restrict the items of the array 
(OpenAPI 3: {@code items.enum}); an
+ * {@code enum} on the array schema itself would only accept array values 
equal to one of the strings or numbers, that
+ * is, no array at all.
+ */
+class RestOpenApiReaderArrayAllowableValuesTest extends CamelTestSupport {
+
+    @BindToRegistry("dummy-rest")
+    private DummyRestConsumerFactory factory = new DummyRestConsumerFactory();
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                rest("/colors")
+                        .get("/filter")
+                        
.param().name("colors").type(RestParamType.query).dataType("array").arrayType("string")
+                        .allowableValues("red", "green", "blue").endParam()
+                        
.param().name("sizes").type(RestParamType.query).dataType("array").arrayType("integer")
+                        .allowableValues("1", "2", "3").endParam()
+                        
.param().name("mode").type(RestParamType.query).dataType("string")
+                        .allowableValues("fast", "slow").endParam()
+                        .to("log:filter");
+            }
+        };
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "3.1", "3.0" })
+    void testArrayAllowableValuesAreOnItems(String version) throws Exception {
+        BeanConfig config = new BeanConfig();
+        config.setHost("localhost:8080");
+        config.setSchemes(new String[] { "http" });
+        config.setBasePath("/api");
+        config.setVersion(version);
+        RestOpenApiReader reader = new RestOpenApiReader();
+
+        OpenAPI openApi = reader.read(context, context.getRestDefinitions(), 
config, context.getName(),
+                new DefaultClassResolver());
+        assertNotNull(openApi);
+
+        List<Parameter> parameters = 
openApi.getPaths().get("/colors/filter").getGet().getParameters();
+
+        Schema<?> colors = schemaOf(parameters, "colors");
+        assertNull(colors.getEnum(), "the array schema of 'colors' must not 
have an enum");
+        assertEquals(List.of("red", "green", "blue"), 
colors.getItems().getEnum(), "the items of 'colors'");
+
+        Schema<?> sizes = schemaOf(parameters, "sizes");
+        assertNull(sizes.getEnum(), "the array schema of 'sizes' must not have 
an enum");
+        assertEquals(List.of(1, 2, 3), sizes.getItems().getEnum(), "the items 
of 'sizes'");
+
+        // a parameter that is not an array keeps its enum
+        assertEquals(List.of("fast", "slow"), schemaOf(parameters, 
"mode").getEnum());
+
+        // the same in the JSON document written by the mapper of this OpenAPI 
version
+        String json = RestOpenApiSupport.getJsonFromOpenAPIAsString(openApi, 
config);
+        JsonNode jsonParameters = new 
ObjectMapper().readTree(json).path("paths").path("/colors/filter").path("get")
+                .path("parameters");
+
+        JsonNode colorsJson = jsonSchemaOf(jsonParameters, "colors");
+        assertEquals("array", colorsJson.path("type").asText(), json);
+        assertFalse(colorsJson.has("enum"), "the array schema of 'colors' must 
not have an enum in " + json);
+        assertEquals("[\"red\",\"green\",\"blue\"]", 
colorsJson.path("items").path("enum").toString(), json);
+
+        JsonNode sizesJson = jsonSchemaOf(jsonParameters, "sizes");
+        assertEquals("array", sizesJson.path("type").asText(), json);
+        assertFalse(sizesJson.has("enum"), "the array schema of 'sizes' must 
not have an enum in " + json);
+        assertEquals("[1,2,3]", 
sizesJson.path("items").path("enum").toString(), json);
+
+        assertEquals("[\"fast\",\"slow\"]", jsonSchemaOf(jsonParameters, 
"mode").path("enum").toString(), json);
+
+        context.stop();
+    }
+
+    private static Schema<?> schemaOf(List<Parameter> parameters, String name) 
{
+        return parameters.stream().filter(p -> 
name.equals(p.getName())).findFirst().orElseThrow().getSchema();
+    }
+
+    private static JsonNode jsonSchemaOf(JsonNode parameters, String name) {
+        for (JsonNode parameter : parameters) {
+            if (name.equals(parameter.path("name").asText())) {
+                return parameter.path("schema");
+            }
+        }
+        throw new AssertionError("no parameter " + name + " in " + parameters);
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
index 29ccc2f0b581..fb3a926c22c2 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
@@ -2152,6 +2152,13 @@ value, as with the Rest DSL: `/items/caf%C3%A9` sets the 
`id` header to `café`,
 decoded on its own (an encoded `/` stays in its parameter) and a `+` stays a 
`+`. A value with a malformed escape is
 kept as it is. Routes that decoded these headers themselves must no longer do 
so.
 
+=== camel-openapi-java - allowable values of an array parameter
+
+The OpenAPI document generated from the Rest DSL now puts the allowable values 
of a parameter with
+`dataType("array")` on the items of the array (`items.enum`), as they restrict 
the values of the items. They were put
+on the array schema itself (`enum` next to `type: array`), which does not 
accept any array, so tools that validate
+requests or generate clients from the document rejected or mistyped the 
parameter.
+
 === camel-jbang
 
 The `--runtime` option of `camel run` has a new default value `jbang`, which 
is the existing in-process

Reply via email to