HelloThere
HelloThere

Reputation: 71

Problems with using oneOf in @Schema annotation with springdoc

I have the next code in my Spring Boot app:

@Schema(description = "Example field",
        anyOf = {
            ExampleResponse.class,
            AnotherExampleResponse.class
        }
)
private Object field; 

and I expect that there should be two optional types in swagger-ui. But there is no types at all. What am I doing wrong?

Example

Upvotes: 3

Views: 9714

Answers (2)

Salvatore Di Palo
Salvatore Di Palo

Reputation: 55

You can use an interface that you will implement in the various classes

@Schema(
  oneOf = {
    ExampleResponse.class,
    AnotherExampleResponse.class
  }
)
public interface OneOfExampleResponseAnotherExampleResponse {}

Below are the various reference models

public class ExampleResponse implements OneOfExampleResponseAnotherExampleResponse {

}
public class AnotherExampleResponse implements OneOfExampleResponseAnotherExampleResponse {

}

Now it will be enough to declare a field of type OneOfExampleResponseAnotherExampleResponse

private OneOfExampleResponseAnotherExampleResponse field;

Upvotes: 2

HelloThere
HelloThere

Reputation: 71

I found an answer. I used oneOf property uncorrectly: i had to use it in a definition of API and inside of @ApiResponce annotation like this:

@ApiResponse(responseCode = "403",
description = "Example",
content = @Content(schema = @Schema( oneOf = 
{ 
 ExampleResponse.class, 
 ErrorResponse.class
}
)))

Upvotes: 3

Related Questions