Skip to content

Commit ea87334

Browse files
committed
Change report: #1799.
1 parent 93397fe commit ea87334

File tree

14 files changed

+381
-12
lines changed

14 files changed

+381
-12
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
8888
import org.springframework.boot.actuate.autoconfigure.web.server.ConditionalOnManagementPort;
8989
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
90+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
9091
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
9192
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
9293
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
@@ -169,6 +170,7 @@ AdditionalModelsConverter additionalModelsConverter(ObjectMapperProvider objectM
169170
*/
170171
@Bean
171172
@Lazy(false)
173+
@ConditionalOnBean(PropertyCustomizer.class)
172174
PropertyCustomizingConverter propertyCustomizingConverter(Optional<List<PropertyCustomizer>> customizers) {
173175
return new PropertyCustomizingConverter(customizers);
174176
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/converters/PolymorphicModelConverter.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import io.swagger.v3.core.converter.ModelConverterContext;
3737
import io.swagger.v3.core.util.AnnotationsUtils;
3838
import io.swagger.v3.oas.models.media.ComposedSchema;
39+
import io.swagger.v3.oas.models.media.ObjectSchema;
3940
import io.swagger.v3.oas.models.media.Schema;
4041
import org.springdoc.core.providers.ObjectMapperProvider;
4142

@@ -61,14 +62,29 @@ public PolymorphicModelConverter(ObjectMapperProvider springDocObjectMapper) {
6162

6263
@Override
6364
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
64-
if (chain.hasNext()) {
65-
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
66-
if (resolvedSchema == null || resolvedSchema.get$ref() == null) return resolvedSchema;
67-
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
65+
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
66+
if (javaType != null) {
67+
if (chain.hasNext()) {
68+
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
69+
resolvedSchema = getResolvedSchema(javaType, resolvedSchema);
70+
if (resolvedSchema == null || resolvedSchema.get$ref() == null)
71+
return resolvedSchema;
72+
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
73+
}
6874
}
6975
return null;
7076
}
7177

78+
private static Schema<?> getResolvedSchema(JavaType javaType, Schema<?> resolvedSchema) {
79+
if (resolvedSchema instanceof ObjectSchema && resolvedSchema.getProperties() != null){
80+
if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getName()))
81+
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getName());
82+
else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSimpleName()))
83+
resolvedSchema = resolvedSchema.getProperties().get(javaType.getRawClass().getSimpleName());
84+
}
85+
return resolvedSchema;
86+
}
87+
7288
/**
7389
* Compose polymorphic schema schema.
7490
*

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SpringDocAnnotationsUtils.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,7 @@ private static MediaType getMediaType(Schema schema, Components components, Json
356356
Schema oSchema = mediaType.getSchema();
357357
for (SchemaProperty sp: annotationContent.schemaProperties()) {
358358
Class<?> schemaImplementation = sp.schema().implementation();
359-
boolean isArray = false;
360-
if (schemaImplementation == Void.class) {
361-
schemaImplementation = sp.array().schema().implementation();
362-
if (schemaImplementation != Void.class) {
363-
isArray = true;
364-
}
365-
}
359+
boolean isArray = isArray(annotationContent);
366360
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation)
367361
.ifPresent(s -> {
368362
if ("array".equals(oSchema.getType())) {

springdoc-openapi-starter-webmvc-api/src/test/java/test/org/springdoc/api/v30/app192/SpringDocApp192Test.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,12 @@
2525

2626
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
2727

28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
2829

29-
public class SpringDocApp192Test extends AbstractSpringDocV30Test { }
30+
31+
public class SpringDocApp192Test extends AbstractSpringDocV30Test {
32+
33+
@SpringBootApplication
34+
static class SpringDocTestApp {}
35+
36+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import com.fasterxml.jackson.annotation.JsonSubTypes;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import io.swagger.v3.oas.annotations.media.Schema;
7+
8+
/**
9+
* Interface of the Animal.
10+
*/
11+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
12+
@JsonSubTypes({
13+
@JsonSubTypes.Type(value = Dog.class, name = "dog"),
14+
@JsonSubTypes.Type(value = Cat.class, name = "cat"),
15+
})
16+
@Schema(description = "Represents an Animal class.")
17+
public interface Animal {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
5+
import org.springframework.http.HttpStatus;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.ResponseStatus;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping(path = "/")
13+
public class BasicController {
14+
15+
@GetMapping("/test")
16+
@ResponseStatus(HttpStatus.OK)
17+
@Operation(summary = "get", description = "Provides a list of books.")
18+
public Knowledge get() {
19+
return new Books(new Book("Introduction to algorithms"));
20+
}
21+
22+
@GetMapping("/test1")
23+
@Operation(summary = "get1", description = "Provides an animal.")
24+
public Animal get1() {
25+
26+
return new Dog("Foo", 12);
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
6+
@Schema(description = "Represents a Book.")
7+
public class Book {
8+
9+
private String title;
10+
11+
public Book(String title) {
12+
this.title = title;
13+
}
14+
15+
public String getTitle() {
16+
return title;
17+
}
18+
19+
public void setTitle(String title) {
20+
this.title = title;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
7+
import io.swagger.v3.oas.annotations.media.Schema;
8+
9+
@Schema(description = "Represents a list of Books.")
10+
public class Books extends ArrayList<Book> implements Knowledge {
11+
12+
public Books() {
13+
super();
14+
}
15+
16+
public Books(Book... books) {
17+
this();
18+
19+
if (books != null) {
20+
Arrays.stream(books).forEach(this::add);
21+
}
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
6+
@Schema(description = "Represents a Cat class.")
7+
public class Cat implements Animal {
8+
9+
private Integer speed;
10+
11+
public Cat(Integer speed) {
12+
this.speed = speed;
13+
}
14+
15+
public Integer getSpeed() {
16+
return speed;
17+
}
18+
19+
public void setSpeed(Integer speed) {
20+
this.speed = speed;
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
6+
@Schema(description = "Represents a Dog class.")
7+
public class Dog implements Animal {
8+
9+
private String name;
10+
private Integer age;
11+
12+
public Dog(String name, Integer age) {
13+
this.name = name;
14+
this.age = age;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public Integer getAge() {
26+
return age;
27+
}
28+
29+
public void setAge(Integer age) {
30+
this.age = age;
31+
}
32+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5+
import io.swagger.v3.oas.annotations.media.Schema;
6+
7+
/**
8+
* Interface of the Knowledge.
9+
*/
10+
@Schema(description = "Represents the knowledge.")
11+
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_OBJECT)
12+
@JsonSubTypes({@JsonSubTypes.Type(value = Books.class)})
13+
public interface Knowledge {
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test.org.springdoc.api.v30.app193;
2+
3+
public class Response {
4+
5+
private Knowledge books;
6+
7+
public Response(Knowledge books) {
8+
this.books = books;
9+
}
10+
11+
public Knowledge getBooks() {
12+
return books;
13+
}
14+
15+
public void setBooks(Knowledge books) {
16+
this.books = books;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*/
22+
23+
package test.org.springdoc.api.v30.app193;
24+
25+
26+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
30+
31+
public class SpringDocApp193Test extends AbstractSpringDocV30Test {
32+
33+
@SpringBootApplication
34+
static class SpringDocTestApp {}
35+
36+
}

0 commit comments

Comments
 (0)