Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gh-nalo opened this issue Oct 23, 2023 · 9 comments
Open

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

gh-nalo opened this issue Oct 23, 2023 · 9 comments
Labels
added to backlog The issue was added to backlog area:client This item is related to the client extension bug Something isn't working pinned Issues and PRs that must not stale

Comments

@gh-nalo
Copy link

gh-nalo commented Oct 23, 2023

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?

@hbelmiro hbelmiro added added to backlog The issue was added to backlog bug Something isn't working pinned Issues and PRs that must not stale labels Oct 23, 2023
@ricardozanini
Copy link
Member

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
Copy link
Contributor

@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
Copy link
Author

gh-nalo commented Oct 26, 2023

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

@ricardozanini
Copy link
Member

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

jeffersoncleyson pushed a commit to jeffersoncleyson/quarkus-openapi-generator that referenced this issue Dec 11, 2023
@jeffersoncleyson
Copy link

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

@ractoc
Copy link

ractoc commented Feb 22, 2024

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
Copy link
Member

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

@ricardozanini ricardozanini added the area:client This item is related to the client extension label Feb 27, 2024
@wernert75
Copy link

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
added to backlog The issue was added to backlog area:client This item is related to the client extension bug Something isn't working pinned Issues and PRs that must not stale
Projects
None yet
7 participants