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 76d70c0905a9 CAMEL-25663: camel-rest-openapi - mock mode no longer
NPEs for an operation without a 200 response (#27897)
76d70c0905a9 is described below
commit 76d70c0905a9f6f18f67a90243ca7e3852635406
Author: Andrea Cosentino <[email protected]>
AuthorDate: Thu Oct 15 16:38:09 2026 +0200
CAMEL-25663: camel-rest-openapi - mock mode no longer NPEs for an operation
without a 200 response (#27897)
Co-Authored-By: Claude Opus 4.8 <[email protected]>
---
.../DefaultRestOpenapiProcessorStrategy.java | 8 +++++--
.../openapi/RestOpenapiProcessorStrategyTest.java | 25 ++++++++++++++++++++++
2 files changed, 31 insertions(+), 2 deletions(-)
diff --git
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
index 9478ecad5ba8..bd27d14c96cd 100644
---
a/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
+++
b/components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/DefaultRestOpenapiProcessorStrategy.java
@@ -269,7 +269,8 @@ public class DefaultRestOpenapiProcessorStrategy extends
ServiceSupport
});
}
- private void loadMockData(Operation operation, String verb, String path,
Exchange exchange) {
+ // package-private for testing (CAMEL-25663)
+ void loadMockData(Operation operation, String verb, String path, Exchange
exchange) {
final PackageScanResourceResolver resolver =
PluginHelper.getPackageScanResourceResolver(camelContext);
final String[] includes = mockIncludePattern != null ?
mockIncludePattern.split(",") : null;
@@ -333,8 +334,11 @@ public class DefaultRestOpenapiProcessorStrategy extends
ServiceSupport
String contentType = ExchangeHelper.getContentType(exchange);
String accept = exchange.getMessage().getHeader("Accept",
String.class);
if (operation.getResponses() != null) {
+ // CAMEL-25663: an operation whose responses have no 200
(for example only 201, 204, default or
+ // 2XX) has a null ApiResponse here; treat it like a
response without content so the mock returns
+ // an empty 204 body instead of throwing a
NullPointerException
ApiResponse a = operation.getResponses().get("200");
- Content c = a.getContent();
+ Content c = a != null ? a.getContent() : null;
if (c != null && !c.isEmpty()) {
// prefer media-type that is the same as the incoming
content-type
// if none found, then find first matching
content-type from the HTTP Accept header
diff --git
a/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
index 54dc21d2d39e..96ab4f93032b 100644
---
a/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
+++
b/components/camel-rest-openapi/src/test/java/org/apache/camel/component/rest/openapi/RestOpenapiProcessorStrategyTest.java
@@ -18,12 +18,16 @@ package org.apache.camel.component.rest.openapi;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
+import io.swagger.v3.oas.models.responses.ApiResponse;
+import io.swagger.v3.oas.models.responses.ApiResponses;
import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.platform.http.PlatformHttpComponent;
import org.apache.camel.component.platform.http.spi.PlatformHttpConsumerAware;
import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -88,6 +92,27 @@ class RestOpenapiProcessorStrategyTest extends
ManagedCamelTestSupport {
assertEquals("GENOPID_GET.user._id_", userByIdOp.getOperationId());
}
+ @Test
+ void testMockModeWithoutOkResponseReturnsEmpty204() {
+ // CAMEL-25663: an operation whose responses have no 200 (only
default, 201, 204, ...) must not throw a
+ // NullPointerException in mock mode; with no mock data it returns an
empty 204.
+ DefaultRestOpenapiProcessorStrategy strategy = new
DefaultRestOpenapiProcessorStrategy();
+ strategy.setCamelContext(camelContext);
+
+ Operation onlyDefault = new Operation()
+ .responses(new ApiResponses().addApiResponse("default", new
ApiResponse()));
+ Exchange exchange = new DefaultExchange(camelContext);
+ assertDoesNotThrow(() -> strategy.loadMockData(onlyDefault, "GET",
"/x", exchange));
+ assertEquals(204,
exchange.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE, int.class));
+ assertEquals("", exchange.getMessage().getBody());
+
+ Operation only201 = new Operation()
+ .responses(new ApiResponses().addApiResponse("201", new
ApiResponse()));
+ Exchange exchange201 = new DefaultExchange(camelContext);
+ assertDoesNotThrow(() -> strategy.loadMockData(only201, "GET", "/x",
exchange201));
+ assertEquals(204,
exchange201.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE, int.class));
+ }
+
private OpenAPI getOpenApi() {
return RestOpenApiEndpoint.loadSpecificationFrom(camelContext,
"missing-opid.yaml");
}