You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Bjorn Beskow (Jira)" <ji...@apache.org> on 2025/05/31 07:56:00 UTC

[jira] [Commented] (CAMEL-22116) camel-openapi-validator doesn't work for contract-first api's

    [ https://issues.apache.org/jira/browse/CAMEL-22116?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17955371#comment-17955371 ] 

Bjorn Beskow commented on CAMEL-22116:
--------------------------------------

As Claus noted, It might seem backwards to fix the problem in camel-openapi-validator when the origin of the problem appears to be in camel-platform-http. It is however not clear to me how this can be corrected at the source (org.apache.camel.http.common.DefaultHttpBinding):
{code:java}
public void readRequest(HttpServletRequest request, Message message) {
    ...
    String rawPath = getRawPath(request);
    ...
    headers.put(Exchange.HTTP_PATH, rawPath);{code}
 
{code:java}
private String getRawPath(HttpServletRequest request) {
    String uri = request.getRequestURI();
    /**
     * In async case, it seems that request.getContextPath() can return null
     *
     * @see https://dev.eclipse.org/mhonarc/lists/jetty-users/msg04669.html
     */
    String contextPath = request.getContextPath() == null ? "" : request.getContextPath();
    String servletPath = request.getServletPath() == null ? "" : request.getServletPath();
    return uri.substring(contextPath.length() + servletPath.length());
}
 {code}
In the context of a rest-openapi request, it seems to me request.getServletPath() will always evaluate to the full request path (i.e. full uri minus contextPath), and hence HTTP_PATH will always be set to ''. But I assume that it is correctly set in other situations?

Hence I don't know how to proceed.

> camel-openapi-validator doesn't work for contract-first api's
> -------------------------------------------------------------
>
>                 Key: CAMEL-22116
>                 URL: https://issues.apache.org/jira/browse/CAMEL-22116
>             Project: Camel
>          Issue Type: Bug
>          Components: rest
>    Affects Versions: 4.10.4, 4.11.0
>            Reporter: Bjorn Beskow
>            Priority: Minor
>             Fix For: 4.10.6, 4.13.0
>
>
> Using camel-openapi-validator with an contract-first api such as [https://github.com/apache/camel-spring-boot-examples/openapi-contract-first] results in an HTTP 404 for all endpoints:
>  
> {code:java}
> > curl http://localhost:8080/api/v3/pet/1
> Validation failed.
> [ERROR][GET ] No API path found that matches request ''.{code}
> This is caused by the CamelHttpPath set to '' by camel-platform-http, which is used to select endpoint from the openapi specification.
> The "CamelPlatformHttpContextPath" is however correctly set, and hence the correct value for path can be calculated:
> {code:java}
> String path = exchange.getMessage().getHeader(Exchange.HTTP_PATH, String.class);
> // Workaround for camel-platform-http which does not properly set HTTP_PATH header
> if (StringUtils.isEmpty(path)) {
>     String uri = exchange.getMessage().getHeader(Exchange.HTTP_URI, String.class);
>     String contextPath = exchange.getMessage().getHeader("CamelPlatformHttpContextPath", String.class);
>     if (StringUtils.isNotEmpty(contextPath) && uri.startsWith(contextPath)) {
>         path = uri.substring(contextPath.length());
>     }
> } {code}
> With path correctly set, the corresponding endpoint can be found in the openapi specification, which corrects the problem.
>  
> I'll submit a merge request with the above patch.
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)