springfox icon indicating copy to clipboard operation
springfox copied to clipboard

@Schema oneOf, anyOf, etc. and better support for inheritance

Open tsathis opened this issue 5 years ago • 6 comments

Can we use polymorphism features like oneOf, anyOf etc with Springfox?

@ApiResponse( content = @Content( 
                    mediaType = MediaType.APPLICATION_JSON_VALUE, 
                    schema = @Schema(oneOf = { Bird.class, Cat.class })) )
@PostMapping( path = "animals", produces = "application/json" )
ResponseEntity<IAnimal> echoAnimal( @RequestBody IAnimal animal );

I used io.swagger.v3.oas.annotations.responses.ApiResponse. But did not work. Can anyone please help me to do it? Note: I am using springfox-boot-starter:3.0.0-SNAPSHOT.

tsathis avatar Jul 13 '20 12:07 tsathis

Not yet. This will be available in the next release

dilipkrish avatar Jul 13 '20 12:07 dilipkrish

@dilipkrish thanks for the quick reply. Will 3.0.0 be released on tomorrow?

tsathis avatar Jul 13 '20 12:07 tsathis

Yes will be pushing it today! 🎉

dilipkrish avatar Jul 13 '20 12:07 dilipkrish

@dilipkrish Thanks for the new release. I updated to the new release, but still, I could not succeed what I was trying to do (maybe my is approach wrong).

I needed to show Cat and Bird as possible inputs for IAnimal and I used oneOf,

@PostMapping( path = "animals", produces = "application/json" )
ResponseEntity<IAnimal> echoAnimal( @io.swagger.v3.oas.annotations.parameters.RequestBody(
            content = @Content(
                    mediaType = MediaType.APPLICATION_JSON_VALUE,
                    schema = @Schema( oneOf = { Bird.class, Cat.class } ), examples = {
                    @ExampleObject( name = "Cat",
                            value = "{\n" +
                                            "  \"name\": \"Kitty\",\n" +
                                            "  \"id\": 034,\n" +
                                            "  \"runningSpeed\": 34.0\n" +
                                            "}" )
            } ) ) @RequestBody  IAnimal animal );

But the result is, image

In my pom.xml,

<repositories>
        <repository>
            <id>jcenter-snapshots</id>
            <name>jcenter</name>
            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
        </repository>
</repositories>
<dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
</dependencies>

Expected result:

image image

tsathis avatar Jul 14 '20 10:07 tsathis

Hi @dilipkrish, What is the status of this polymorphic feature?

amilacjay avatar Apr 20 '21 03:04 amilacjay

This worked for me. I just annotated base class with //java @Schema( oneOf = { Bird.class, Cat.class } ) //kotlin @Schema( oneOf = [Bird.class,Cat.class ])

I think we should close this to avoid confusion that this feature is not available yet.

rajpra8 avatar Oct 16 '24 02:10 rajpra8