Skip to content

Commit 2eccb93

Browse files
committed
Allow customizing / disabling PolymorphicModelConverter. Fixes #1334
1 parent 58272e7 commit 2eccb93

File tree

8 files changed

+356
-0
lines changed

8 files changed

+356
-0
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public final class Constants {
9090
*/
9191
public static final String SPRINGDOC_PAGEABLE_CONVERTER_ENABLED = "springdoc.model-converters.pageable-converter.enabled";
9292

93+
/**
94+
* The constant SPRINGDOC_POLYMORPHIC_CONVERTER_ENABLED.
95+
*/
96+
public static final String SPRINGDOC_POLYMORPHIC_CONVERTER_ENABLED = "springdoc.model-converters.polymorphic-converter.enabled";
97+
9398
/**
9499
* The constant SPRINGDOC_SCHEMA_RESOLVE_PROPERTIES.
95100
*/

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocConfigProperties.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ public static class ModelConverters {
674674
*/
675675
private PageableConverter pageableConverter = new PageableConverter();
676676

677+
/**
678+
* The Polymorphic model converter.
679+
*/
680+
private PolymorphicConverter polymorphicConverter = new PolymorphicConverter();
681+
682+
677683
/**
678684
* Gets deprecating converter.
679685
*
@@ -710,6 +716,24 @@ public void setPageableConverter(PageableConverter pageableConverter) {
710716
this.pageableConverter = pageableConverter;
711717
}
712718

719+
/**
720+
* Gets polymorphic model converter.
721+
*
722+
* @return the polymorphic model converter
723+
*/
724+
public PolymorphicConverter getPolymorphicConverter() {
725+
return polymorphicConverter;
726+
}
727+
728+
/**
729+
* Sets polymorphic model converter.
730+
*
731+
* @param polymorphicConverter the polymorphic model converter
732+
*/
733+
public void setPolymorphicConverter(PolymorphicConverter polymorphicConverter) {
734+
this.polymorphicConverter = polymorphicConverter;
735+
}
736+
713737
/**
714738
* The type Deprecating converter.
715739
* @author bnasslashen
@@ -740,6 +764,36 @@ public void setEnabled(boolean enabled) {
740764
}
741765
}
742766

767+
/**
768+
* The type Polymorphic model converter.
769+
*/
770+
public static class PolymorphicConverter {
771+
772+
/**
773+
* The Enabled.
774+
*/
775+
private boolean enabled;
776+
777+
/**
778+
* Is enabled boolean.
779+
*
780+
* @return the boolean
781+
*/
782+
public boolean isEnabled() {
783+
return enabled;
784+
}
785+
786+
/**
787+
* Sets enabled.
788+
*
789+
* @param enabled the enabled
790+
*/
791+
public void setEnabled(boolean enabled) {
792+
this.enabled = enabled;
793+
}
794+
}
795+
796+
743797
/**
744798
* The type Pageable converter.
745799
* @author bnasslashen

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import static org.springdoc.core.Constants.SPRINGDOC_DEPRECATING_CONVERTER_ENABLED;
8181
import static org.springdoc.core.Constants.SPRINGDOC_ENABLED;
8282
import static org.springdoc.core.Constants.SPRINGDOC_PAGEABLE_CONVERTER_ENABLED;
83+
import static org.springdoc.core.Constants.SPRINGDOC_POLYMORPHIC_CONVERTER_ENABLED;
8384
import static org.springdoc.core.Constants.SPRINGDOC_SCHEMA_RESOLVE_PROPERTIES;
8485
import static org.springdoc.core.Constants.SPRINGDOC_SHOW_ACTUATOR;
8586
import static org.springdoc.core.SpringDocUtils.getConfig;
@@ -193,6 +194,7 @@ PageableOpenAPIConverter pageableOpenAPIConverter() {
193194
*/
194195
@Bean
195196
@ConditionalOnMissingBean
197+
@ConditionalOnProperty(name = SPRINGDOC_POLYMORPHIC_CONVERTER_ENABLED, matchIfMissing = true)
196198
@Lazy(false)
197199
PolymorphicModelConverter polymorphicModelConverter() {
198200
return new PolymorphicModelConverter();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package test.org.springdoc.api.app168;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
7+
8+
@JsonTypeInfo(use = Id.NAME, property = "type")
9+
@JsonSubTypes({
10+
@Type(ChildOfAbstract1.class),
11+
@Type(ChildOfAbstract2.class)
12+
})
13+
public abstract class AbstractParent {
14+
private int id;
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
}
24+
25+
class ChildOfAbstract1 extends AbstractParent {
26+
private String abstrachChild1Param;
27+
28+
public String getAbstrachChild1Param() {
29+
return abstrachChild1Param;
30+
}
31+
32+
public void setAbstrachChild1Param(String abstrachChild1Param) {
33+
this.abstrachChild1Param = abstrachChild1Param;
34+
}
35+
}
36+
37+
class ChildOfAbstract2 extends AbstractParent {
38+
private String abstractChild2Param;
39+
40+
public String getAbstractChild2Param() {
41+
return abstractChild2Param;
42+
}
43+
44+
public void setAbstractChild2Param(String abstractChild2Param) {
45+
this.abstractChild2Param = abstractChild2Param;
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package test.org.springdoc.api.app168;
2+
3+
import com.fasterxml.jackson.annotation.JsonSubTypes;
4+
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
7+
8+
@JsonTypeInfo(use = Id.NAME, property = "type")
9+
@JsonSubTypes({
10+
@Type(ChildOfConcrete1.class),
11+
@Type(ChildOfConcrete2.class)
12+
})
13+
public class ConcreteParent {
14+
private int id;
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
}
24+
25+
class ChildOfConcrete1 extends ConcreteParent {
26+
private String concreteChild1Param;
27+
28+
public String getConcreteChild1Param() {
29+
return concreteChild1Param;
30+
}
31+
32+
public void setConcreteChild1Param(String concreteChild1Param) {
33+
this.concreteChild1Param = concreteChild1Param;
34+
}
35+
}
36+
37+
class ChildOfConcrete2 extends ConcreteParent {
38+
private String concreteChild2Param;
39+
40+
public String getConcreteChild2Param() {
41+
return concreteChild2Param;
42+
}
43+
44+
public void setConcreteChild2Param(String concreteChild2Param) {
45+
this.concreteChild2Param = concreteChild2Param;
46+
}
47+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package test.org.springdoc.api.app168;
2+
3+
import java.util.List;
4+
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("class-hierarchy")
12+
public class Controller {
13+
@PostMapping("abstract-parent")
14+
public Response abstractParent(@RequestBody AbstractParent payload) {
15+
return null;
16+
}
17+
18+
@PostMapping("concrete-parent")
19+
public Response concreteParent(@RequestBody ConcreteParent payload) {
20+
return null;
21+
}
22+
}
23+
24+
class Response {
25+
AbstractParent abstractParent;
26+
27+
List<ConcreteParent> concreteParents;
28+
29+
public AbstractParent getAbstractParent() {
30+
return abstractParent;
31+
}
32+
33+
public void setAbstractParent(AbstractParent abstractParent) {
34+
this.abstractParent = abstractParent;
35+
}
36+
37+
public List<ConcreteParent> getConcreteParents() {
38+
return concreteParents;
39+
}
40+
41+
public void setConcreteParents(List<ConcreteParent> concreteParents) {
42+
this.concreteParents = concreteParents;
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.org.springdoc.api.app168;
2+
3+
import java.util.Optional;
4+
5+
import io.swagger.v3.core.converter.ModelConverter;
6+
import io.swagger.v3.core.converter.ModelConverters;
7+
import org.springdoc.core.Constants;
8+
import org.springdoc.core.converters.PolymorphicModelConverter;
9+
import test.org.springdoc.api.AbstractSpringDocTest;
10+
11+
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
import org.springframework.test.context.TestPropertySource;
13+
14+
15+
@TestPropertySource(properties = Constants.SPRINGDOC_POLYMORPHIC_CONVERTER_ENABLED + "=false")
16+
public class SpringDocApp168Test extends AbstractSpringDocTest {
17+
18+
@SpringBootApplication
19+
static class SpringDocTestApp {}
20+
21+
static {
22+
Optional<ModelConverter> modelConverterOptional =
23+
ModelConverters.getInstance().getConverters()
24+
.stream().filter(modelConverter -> modelConverter instanceof PolymorphicModelConverter).findAny();
25+
modelConverterOptional.ifPresent(ModelConverters.getInstance()::removeConverter);
26+
}
27+
28+
}

0 commit comments

Comments
 (0)