quarkiverse / quarkus-openapi-generator

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

server extension: HTTP status 201 instead of 200 for POST? #670

Open markuszoeller opened 4 months ago

markuszoeller commented 4 months ago

I'm using a locally built jar for the server generation: https://github.com/quarkiverse/quarkus-openapi-generator/pull/482#issuecomment-1964132436 and use the example of POST /pet defined in https://github.com/quarkiverse/quarkus-openapi-generator/blob/a8e255cb480b3cb3ea24e1079725a48ff81d3958/server/deployment/src/test/resources/openapi/petstore-openapi.json#L112-L155

I'd rather have a HTTP status code of "201 Created" instead of "200 OK" for when the resource got created but it seems I cannot influence that. The generated code looks like this:

// [...]

@Path("/pet")
public interface PetResource {

  // [...]

  /**
   * <p>
   * Add a new pet to the store
   * </p>
   * 
   */
  @POST
  @Produces({"application/xml", "application/json"})
  @Consumes({"application/xml", "application/json", "application/x-www-form-urlencoded"})
  Pet addPet(@NotNull Pet data);

// [...]

Even when I change it in the spec to "201" like this:

      "post": {
        "tags": [
          "pet"
        ],
        "summary": "Add a new pet to the store",
        "description": "Add a new pet to the store",
        "operationId": "addPet",
        "requestBody": {
          "description": "Create a new pet in the store",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Pet"
              }
            },
            "application/xml": {
              "schema": {
                "$ref": "#/components/schemas/Pet"
              }
            },
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/Pet"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {

the swagger UI still says it returns a 200 (same if I try it with curl):

image

In the quarkus docs (https://quarkus.io/guides/resteasy-reactive), the ResponseBuilder is shown to set all kinds of details, including HTTP status code and for a POST, the location: https://javadoc.io/doc/io.quarkus.resteasy.reactive/resteasy-reactive-common/2.16.0.CR1/org/jboss/resteasy/reactive/RestResponse.html#created(java.net.URI)

How can I change the HTTP response for the generated code?

carlesarnal commented 4 months ago

Hello @markuszoeller, I'm not sure what's your expectation here. If it's for the extension to add the @ApiResponse annotation then that is simply not possible since that annotation is coming from swagger, not from JAX-RS, thus why we're not adding it.

markuszoeller commented 4 months ago

Hello @markuszoeller, I'm not sure what's your expectation here.

Hi @carlesarnal I'd like to have a different HTTP status code for responses to a POST request. Right now the status code in a response to a POST request will be 200, but I'd like to have 201.

Maybe, instead of the currently generated code which returns the bean, here Pet:

@Path("/pet")
public interface PetResource {

  // [...]

  /**
   * <p>
   * Add a new pet to the store
   * </p>
   * 
   */
  @POST
  @Produces({"application/xml", "application/json"})
  @Consumes({"application/xml", "application/json", "application/x-www-form-urlencoded"})
  Pet addPet(@NotNull Pet data);

it could be

  RestResponse<Pet> addPet(@NotNull Pet data);

Does that make any sense? I'm still new to the framework.

carlesarnal commented 3 months ago

Hello @markuszoeller, I'm not sure what's your expectation here.

Hi @carlesarnal I'd like to have a different HTTP status code for responses to a POST request. Right now the status code in a response to a POST request will be 200, but I'd like to have 201.

Maybe, instead of the currently generated code which returns the bean, here Pet:

@Path("/pet")
public interface PetResource {

  // [...]

  /**
   * <p>
   * Add a new pet to the store
   * </p>
   * 
   */
  @POST
  @Produces({"application/xml", "application/json"})
  @Consumes({"application/xml", "application/json", "application/x-www-form-urlencoded"})
  Pet addPet(@NotNull Pet data);

it could be

  RestResponse<Pet> addPet(@NotNull Pet data);

Does that make any sense? I'm still new to the framework.

The generated code is just a stub, so it doesn't return a 200 or a 201, that's up to your implementation. The only way to do that at the interface level is what I shared, using swagger specific annotations, and that is a no-go since it would imply adding another external dependency to the generator. If you're aware of any other way, I would be more than happy to explore it.

github-actions[bot] commented 1 month ago

@ricardozanini @hbelmiro This is being labeled as Stale.