Skip to content

Commit

Permalink
Merge branch 'fix/respect_composed_schemas' of https://github.com/slw…
Browse files Browse the repository at this point in the history
…546/springdoc-openapi into slw546-fix/respect_composed_schemas
  • Loading branch information
bnasslahsen committed Apr 20, 2022
2 parents 7c0703c + dd16cd0 commit c7c31fc
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,16 @@ public static Schema extractSchema(Components components, Type returnType, JsonV
componentSchemas.putAll(schemaMap);
}
else
for (Map.Entry<String, Schema> entry : schemaMap.entrySet())
if (!componentSchemas.containsKey(entry.getKey()))
for (Map.Entry<String, Schema> entry : schemaMap.entrySet()) {
if (!componentSchemas.containsKey(entry.getKey())) {
componentSchemas.put(entry.getKey(), entry.getValue());
// If we've seen this schema before but find later it should be polymorphic,
// replace the existing schema with this richer version.
} else if (entry.getValue() instanceof ComposedSchema &&
!(componentSchemas.get(entry.getKey()) instanceof ComposedSchema)) {
componentSchemas.put(entry.getKey(), entry.getValue());
}
}
components.setSchemas(componentSchemas);
}
if (resolvedSchema.schema != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test.org.springdoc.api.app31;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema
public class Cat extends Pet {

private final boolean meows;

public Cat() {
super();
this.meows = false;
}

public Cat(boolean meows, String name) {
super(name);
this.meows = meows;
}

public boolean getMeows() {
return meows;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test.org.springdoc.api.app31;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema
public class Dog extends Pet {

private final boolean barks;

public Dog() {
super();
this.barks = false;
}

public Dog(boolean barks, String name) {
super(name);
this.barks = barks;
}

public boolean getBarks() {
return barks;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test.org.springdoc.api.app31;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(Dog.class),
@JsonSubTypes.Type(Cat.class)
})
public class Pet {

public final String name;

public Pet() {
this.name = null;
}

public Pet(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test.org.springdoc.api.app31;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PetController {

@GetMapping("/any")
public Pet getAnyPet() {
return new Cat(true, "cat");
}

@GetMapping("/dog")
public Dog getDog() {
return new Dog(true, "dog");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package test.org.springdoc.api.app31;

import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp31Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp {}

}
100 changes: 100 additions & 0 deletions springdoc-openapi-data-rest/src/test/resources/results/app31.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [{
"url": "http://localhost",
"description": "Generated server url"
}],
"paths": {
"/dog": {
"get": {
"tags": ["pet-controller"],
"operationId": "getDog",
"responses": {
"200": {
"description": "OK",
"content": {
"application/hal+json": {
"schema": {
"$ref": "#/components/schemas/Dog"
}
}
}
}
}
}
},
"/any": {
"get": {
"tags": ["pet-controller"],
"operationId": "getAnyPet",
"responses": {
"200": {
"description": "OK",
"content": {
"application/hal+json": {
"schema": {
"oneOf": [{
"$ref": "#/components/schemas/Pet"
}, {
"$ref": "#/components/schemas/Cat"
}, {
"$ref": "#/components/schemas/Dog"
}]
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Dog": {
"type": "object",
"allOf": [{
"$ref": "#/components/schemas/Pet"
}, {
"type": "object",
"properties": {
"barks": {
"type": "boolean"
}
}
}]
},
"Cat": {
"type": "object",
"allOf": [{
"$ref": "#/components/schemas/Pet"
}, {
"type": "object",
"properties": {
"meows": {
"type": "boolean"
}
}
}]
},
"Pet": {
"required": ["type"],
"type": "object",
"properties": {
"name": {
"type": "string"
},
"type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "type"
}
}
}
}
}

0 comments on commit c7c31fc

Please sign in to comment.