quarkiverse / quarkus-openapi-generator

OpenAPI Generator - REST Client Generator
Apache License 2.0
108 stars 69 forks source link

Generate default @Path("/") annotation for client methods #536

Open gh-nalo opened 8 months ago

gh-nalo commented 8 months ago

Using the openapi-generator has caused some minor issues in our application with redirecting calls, because the trailing "/"-slash is missing at the end of requests when there is more than one path defined in the OpenApi spec.

Example 1 - For the following Json:

{
  "swagger": "2.0",
  "info": {
    "version": "1.1.0",
    "title": ""
  },
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json;charset=utf-8"
  ],
  "produces": [
    "application/json;charset=utf-8"
  ],
  "tags": [
    {
      "name": "tag"
    }
  ],
  "paths": {
    "/someMethod/": {
      "post": {
        "operationId": "someMethod",
        "parameters": []
      }
    }
  }
}

this file is generated:

@Path("/someMethod/")
@RegisterRestClient( configKey="someFile_json")
@GeneratedClass(value="someFile.json", tag = "Default")
@ApplicationScoped
public interface DefaultApi {

    @POST
    @GeneratedMethod ("someMethod")
    public javax.ws.rs.core.Response someMethod(
    );

}

here the @POST has the trailing slash due to "/someMethod/" on class level.

Example 2 - But in this case:

{
  "swagger": "2.0",
  "info": {
    "version": "1.1.0",
    "title": ""
  },
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json;charset=utf-8"
  ],
  "produces": [
    "application/json;charset=utf-8"
  ],
  "tags": [
    {
      "name": "tag"
    }
  ],
  "paths": {
    "/someMethod/": {
      "post": {
        "operationId": "someMethod",
        "parameters": []
      }
    },
    "/someMethod/{id}": {
      "post": {
        "operationId": "someMethodId",
        "parameters": []
      }
    }
  }
}

which creates

@Path("/someMethod")
@RegisterRestClient( configKey="someFile_json")
@GeneratedClass(value="someFile.json", tag = "Default")
@ApplicationScoped
public interface DefaultApi {

    @POST
    @GeneratedMethod ("someMethod")
    public javax.ws.rs.core.Response someMethod(
    );

    @POST
    @Path("/{id}")
    @GeneratedMethod ("someMethodId")
    public javax.ws.rs.core.Response someMethodId(
    );
}

The first POST is now missing the "/" at the end after adding another endpoint with - for example - a query parameter. Would it be possible to add a default @Path("/") annotation here for the base-methods?

ricardozanini commented 8 months ago

Thanks for opening the issue, @gh-nalo.

So if I understood correctly, you need the generated code to look like this:

@Path("/someMethod")
@RegisterRestClient( configKey="someFile_json")
@GeneratedClass(value="someFile.json", tag = "Default")
@ApplicationScoped
public interface DefaultApi {

    @POST
    @Path("/")
    @GeneratedMethod ("someMethod")
    public javax.ws.rs.core.Response someMethod(
    );

    @POST
    @Path("/{id}")
    @GeneratedMethod ("someMethodId")
    public javax.ws.rs.core.Response someMethodId(
    );
}

Not sure what the implications are for others. I'm curious about your setup. Why is this not working without the trailing slash at the end? What kind of redirects do you have on your server?

michalsomora commented 8 months ago

@ricardozanini Our setup contains api gateway proxy responsible for intercepting, authenticating and routing of requests to corresponding api provider. If providers expose the api with trailing slash, consumer must provide it too otherwise request cant be matched and api gateway returns 404.

gh-nalo commented 8 months ago

@ricardozanini Yes, that's what I'd expected the generated code to be

ricardozanini commented 8 months ago

@michalsomora @gh-nalo feel free to open a PR.

jeffersoncleyson commented 7 months ago

@ricardozanini Hello, I submit a pull request yesterday. When you are available evaluate my code, please. Thank you!

ractoc commented 4 months ago

Shouldn't it be possible to detect that the original URL has a slash ad the end, and THEN add the @Path("/")? That way, it should only be added when this edge case occurs. Cases where the original URL don't have the slash at the end with then not get one for free unexpectedly.

ricardozanini commented 4 months ago

@ractoc feel free to take a look at the current PR #585. Not sure if @jeffersoncleyson will continue working on it.

wernert75 commented 1 month ago

Are there some news to this issue? I have a similar problem. The external api server responds with 404 if the trailing slash is missing. Bad practice, I think. Is there some solution in next time or a workaround? Thanks.

ricardozanini commented 1 month ago

@wernert75 there's no workaround at the moment. Feel free to send a PR based on #585.