Skip to content

Commit 4035a83

Browse files
committed
No empty description for polymorphic subtypes
- Fixes #2370
1 parent d4f99ac commit 4035a83

File tree

7 files changed

+222
-1
lines changed

7 files changed

+222
-1
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/customizers/JavadocPropertyCustomizer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ else if (resolvedSchema != null && resolvedSchema.get$ref() != null && resolvedS
102102
private void setJavadocDescription(Class<?> cls, List<Field> fields, Schema existingSchema) {
103103
if (existingSchema != null) {
104104
if (StringUtils.isBlank(existingSchema.getDescription())) {
105-
existingSchema.setDescription(javadocProvider.getClassJavadoc(cls));
105+
String classJavadoc = javadocProvider.getClassJavadoc(cls);
106+
if (StringUtils.isNotBlank(classJavadoc)) {
107+
existingSchema.setDescription(classJavadoc);
108+
}
106109
}
107110
Map<String, Schema> properties = existingSchema.getProperties();
108111
if (!CollectionUtils.isEmpty(properties)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test.org.springdoc.api.app170;
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,19 @@
1+
package test.org.springdoc.api.app170;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping(path = "/")
11+
public class BasicController {
12+
13+
@GetMapping("/test1")
14+
@Operation(summary = "get1", description = "Provides an animal.")
15+
public Animal get1() {
16+
17+
return new Dog("Foo", 12);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app170;
2+
3+
4+
import io.swagger.v3.oas.annotations.media.Schema;
5+
6+
@Schema
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package test.org.springdoc.api.app170;
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+
11+
private Integer age;
12+
13+
public Dog(String name, Integer age) {
14+
this.name = name;
15+
this.age = age;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
public Integer getAge() {
27+
return age;
28+
}
29+
30+
public void setAge(Integer age) {
31+
this.age = age;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* * Copyright 2019-2023 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app170;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
/**
26+
* The type Spring doc app 193 test.
27+
*/
28+
public class SpringDocApp170Test extends AbstractSpringDocTest {
29+
30+
/**
31+
* The type Spring doc test app.
32+
*/
33+
@SpringBootApplication
34+
static class SpringDocTestApp {
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test1": {
15+
"get": {
16+
"tags": [
17+
"basic-controller"
18+
],
19+
"summary": "get1",
20+
"description": "Provides an animal.",
21+
"operationId": "get1",
22+
"responses": {
23+
"200": {
24+
"description": "OK",
25+
"content": {
26+
"*/*": {
27+
"schema": {
28+
"oneOf": [
29+
{
30+
"$ref": "#/components/schemas/Cat"
31+
},
32+
{
33+
"$ref": "#/components/schemas/Dog"
34+
}
35+
]
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
},
44+
"components": {
45+
"schemas": {
46+
"Animal": {
47+
"type": "object",
48+
"description": "Represents an Animal class."
49+
},
50+
"Cat": {
51+
"type": "object",
52+
"allOf": [
53+
{
54+
"$ref": "#/components/schemas/Animal"
55+
},
56+
{
57+
"type": "object",
58+
"properties": {
59+
"speed": {
60+
"type": "integer",
61+
"format": "int32"
62+
}
63+
}
64+
}
65+
]
66+
},
67+
"Dog": {
68+
"type": "object",
69+
"description": "Represents a Dog class.",
70+
"allOf": [
71+
{
72+
"$ref": "#/components/schemas/Animal"
73+
},
74+
{
75+
"type": "object",
76+
"properties": {
77+
"name": {
78+
"type": "string"
79+
},
80+
"age": {
81+
"type": "integer",
82+
"format": "int32"
83+
}
84+
}
85+
}
86+
]
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)