Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ComposedSchemas to replace non-composed so we can respect polymorphic links discovered in later methods #1620

Merged
merged 2 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
@@ -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;
}

}
@@ -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;
}

}
@@ -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;
}

}
@@ -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");
}
}
@@ -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
@@ -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"
}
}
}
}
}