swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

OAS3: how to exclude fields in POST operation but not in PUT operation, referencing the same implementation class

Open kremers opened this issue 5 years ago • 3 comments

Hi there, I am working with the annotations https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations

How to create operations that refers to an implementation of some class like @Schema(implementation=....) offering the ID field in case of a PUT and hiding the ID field in case of a POST?

Since ID generation in POST should be up to the server. A PUT should result in 1:1 representation with GET so ID management is up to the client (processing a create or update at server side)

  • https://tools.ietf.org/html/rfc7231#section-4.3.4 “A successful PUT of a given representation would suggest that a subsequent GET on that same target resource will result in an equivalent representation being sent in a 200 (OK) response.”

For POST I like to have something like: @Schema(implementation=MyClass.class, hidden="id") for PUT the full @Schema(implementation=MyClass.class) is ok

I know that I can set @Hidden in MyClass.java, but this will disable the field in both PUT and POST. I can't set it to ReadOnly on MyClass level as well, since PUT needs to write the field.

kremers avatar Jan 16 '20 12:01 kremers

A solution like the one you suggest is not available atm, also currently complex (object) payloads are always resolved as a reference to a schema defined in components, therefore we would end up needing 2 different schemas, one for PUT and one for POST.

That said, assuming you're using some JAX-RS implementation, I am not sure how are you currently mapping your request payload to a representing class used in your method, if you don't want the id at all in your POST I assume you already have 2 different classes..

frantuma avatar Mar 22 '20 12:03 frantuma

This will lead to a lot of additional complexity. A lot of objects that should be handled the same asside of one filed get duplicated, basically ALL entities that are available for PUT.

I would like to feature request this functionality ^^

kremers avatar Mar 24 '20 07:03 kremers

@kremers +1 for this feature request. @frantuma Perhaps having inclusions and exclusions which accepts a set of field names would allow using a Single implementation for multiple POST and PUT resources.

In my case there is a single request object (POJO) for multiple POST resources. Some of the fields are not allowed for some POST resources. Currently, all fields are appearing in the docs. Looking forward for a way to exclude some fields. Can it be possible without creating separate POJO objects?

ipsi-apant avatar Oct 04 '23 20:10 ipsi-apant