davsclaus commented on code in PR #26203: URL: https://github.com/apache/camel/pull/26203#discussion_r3957585272
########## components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiUnmatchedRequestHandler.java: ########## @@ -0,0 +1,44 @@ +/* + * 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.component.rest.openapi; + +import java.util.List; + +import org.apache.camel.Exchange; + +/** + * Used for customizing the HTTP 404 and 405 responses when an incoming request does not match any operation defined in + * the OpenAPI specification. + * <p> + * This allows to plugin different handlers to produce custom error response bodies. + * + * @see DefaultRestOpenApiUnmatchedRequestHandler + * @since 4.23 + */ +public interface RestOpenApiUnmatchedRequestHandler { + + String FACTORY = "rest-openapi-unmatched-request-handler-factory"; Review Comment: **Test gap:** the factory-finder route this constant enables is public contract but has no test coverage — all five new tests go through the registry path. A small test registering a `META-INF/services/org/apache/camel/rest-openapi-unmatched-request-handler-factory` resource would lock the constant's value in. Related, worth being aware of rather than a defect: `CamelContextHelper.findSingleByType` returns `null` when *more than one* handler is registered, so two beans of this type silently fall back to the default with no warning. That is consistent with how `RestClientRequestValidator` behaves, so no change needed in the code — but see my note on the docs. ########## components/camel-rest-openapi/src/main/java/org/apache/camel/component/rest/openapi/RestOpenApiProcessor.java: ########## @@ -47,6 +50,7 @@ public class RestOpenApiProcessor extends AsyncProcessorSupport implements Camel private final String apiContextPath; private final List<RestConsumerContextPathMatcher.ConsumerPath<Operation>> paths = new ArrayList<>(); private final RestOpenapiProcessorStrategy restOpenapiProcessorStrategy; + private RestOpenApiUnmatchedRequestHandler unmatchedRequestHandler; Review Comment: **Defensive nit:** this field is assigned only in `afterPropertiesConfigured()`, and `RestOpenApiEndpoint.createConsumer()` wires that callback *conditionally*: ```java if (consumer instanceof PlatformHttpConsumerAware phca) { phca.registerAfterConfigured(openApiProcessor); } ``` Today `PlatformHttpComponent` is the only `RestOpenApiConsumerFactory` in the tree, so the callback always fires and this is unreachable in practice. But the previous inline code had no such dependency: where it would have degraded to a plain 404, this would NPE. Initialising the field at declaration removes the whole class of risk for free, and `afterPropertiesConfigured()` still overwrites it with the resolved handler. ```suggestion private RestOpenApiUnmatchedRequestHandler unmatchedRequestHandler = new DefaultRestOpenApiUnmatchedRequestHandler(); ``` ########## components/camel-rest-openapi/src/main/docs/rest-openapi-component.adoc: ########## @@ -198,6 +198,40 @@ If any of the validation checks fail, then a `RestOpenApiValidationException` is has a `getValidationErrors` method that returns the error messages from the validator. +== Unmatched requests + +By default, an incoming request that does not match any operation in the OpenAPI specification is answered +with HTTP 404, and a request that matches a path but not the HTTP method is answered with HTTP 405 and an +`Allow` header listing the allowed methods. In both cases the response body is empty. + +To return a custom response, for example a JSON error body, register a bean in the Review Comment: **Docs nit:** this documents only the registry route, but `lookupUnmatchedRequestHandler()` also supports the `META-INF/services` factory-finder route via `RestOpenApiUnmatchedRequestHandler.FACTORY`. It is also worth stating that exactly *one* such bean may be registered — `findSingleByType` returns `null` for two or more, silently falling back to the default handler. One extra sentence here would cover both. (Remember to mirror any change into the `catalog/camel-catalog/.../docs/` copy — you already have that right in this PR.) -- 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]
