1

I have, say, a class Person which, depending on the endpoint, would have its firstName either required or not. Is there a way of doing this with Swagger OpenAPI documentation tags in Java?

I'm imagining it could look a bit like this if it were/is possible:

public class Person {
    @Path("/endpoint1", required = true)
    @Path("/endpoint2", required = false)
    private String firstName;
}
Helen
  • 87,344
  • 17
  • 243
  • 314
Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
  • Does this answer your question? [How to define an optional parameter in path using swagger](https://stackoverflow.com/questions/35011192/how-to-define-an-optional-parameter-in-path-using-swagger) – Nagaraj Tantri Feb 15 '23 at 22:06
  • 1
    @NagarajTantri no. The OP's question is whether a class property can be optional depending on which endpoint this class is used in. – Helen Feb 15 '23 at 22:11

1 Answers1

1

You'll need two classes - one where the firstName property is required, and another one where it's optional. To reduce code duplication, you can define a base class that contains all properties except for firstName and inherit the other two Person classes from this base class.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Sad story. Do you have a documentation reference to ensure this is the case? – Philippe Fanaro Feb 15 '23 at 22:11
  • @PhilippeFanaro Assuming you use Swagger Core 2.x (or the corresponding Springfox/Springdoc version), classes and class properties are annotated with `@Schema`. You can check the [`@Schema` javadoc](https://docs.swagger.io/swagger-core/v2.2.8/apidocs/index.html?io/swagger/v3/oas/annotations/media/Schema.html) to see the supported attributes. Plus, OpenAPI Specification itself does not support varying the required/optional attribute based on paths, it's handled by defining separate schemas (classes) instead. – Helen Feb 15 '23 at 22:17
  • Related discussion / enhancement request in the OpenAPI Specification repository: [Vary presence and requirement of properties with CRUD operation](https://github.com/OAI/OpenAPI-Specification/issues/1497) – Helen Feb 15 '23 at 22:25