Skip to content

Bail sealed class subtype introspection on Schema #2927

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

Merged
merged 1 commit into from
Mar 4, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.v3.core.jackson.SwaggerAnnotationIntrospector;
import io.swagger.v3.oas.annotations.media.Schema;

/**
* The type Spring doc sealed class module.
Expand All @@ -56,6 +58,18 @@ public List<NamedType> findSubtypes(Annotated annotated) {
&& clazz.isSealed()
&& !clazz.getPackage().getName().startsWith("java")
) {

Schema schema = clazz.getAnnotation(Schema.class);
if (schema != null && schema.oneOf().length > 0) {
return new ArrayList<>();
}

JsonSubTypes jsonSubTypes = clazz.getAnnotation(JsonSubTypes.class);
if (jsonSubTypes != null && jsonSubTypes.value().length > 0) {
return new ArrayList<>();
}


Class<?>[] permittedSubClasses = clazz.getPermittedSubclasses();
if (permittedSubClasses.length > 0) {
Arrays.stream(permittedSubClasses).map(NamedType::new).forEach(subTypes::add);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
*
* *
* * *
* * * *
* * * * *
* * * * * * Copyright 2019-2025 the original author or authors.
* * * * * *
* * * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * * you may not use this file except in compliance with the License.
* * * * * * You may obtain a copy of the License at
* * * * * *
* * * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * * *
* * * * * * Unless required by applicable law or agreed to in writing, software
* * * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * * See the License for the specific language governing permissions and
* * * * * * limitations under the License.
* * * * *
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v30.app243;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@PostMapping("/parent")
public void parentEndpoint(@RequestBody SuperClass parent) {

}

}

@Schema(name = SuperClass.SCHEMA_NAME,
discriminatorProperty = "type",
oneOf = {
FirstChildClass.class,
SecondChildClass.class
},
discriminatorMapping = {
@DiscriminatorMapping(value = FirstChildClass.SCHEMA_NAME, schema = FirstChildClass.class),
@DiscriminatorMapping(value = SecondChildClass.SCHEMA_NAME, schema = SecondChildClass.class)
}
)
sealed class SuperClass {

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
public String getType() {
return type;
}

public String type;

public static final String SCHEMA_NAME = "SuperClass";
}

@Schema(name = FirstChildClass.SCHEMA_NAME)
final class FirstChildClass extends SuperClass {

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
public String getType() {
return type;
}

public String type;

public static final String SCHEMA_NAME = "Image";
}

@Schema(name = SecondChildClass.SCHEMA_NAME)
final class SecondChildClass extends SuperClass {

@Schema(requiredMode = Schema.RequiredMode.REQUIRED)
public String getType() {
return type;
}

public String type;

public static final String SCHEMA_NAME = "Mail";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2025 the original author or authors.
* * * * *
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * * you may not use this file except in compliance with the License.
* * * * * You may obtain a copy of the License at
* * * * *
* * * * * https://www.apache.org/licenses/LICENSE-2.0
* * * * *
* * * * * Unless required by applicable law or agreed to in writing, software
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * * See the License for the specific language governing permissions and
* * * * * limitations under the License.
* * * *
* * *
* *
*
*/

package test.org.springdoc.api.v30.app243;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

public class SpringDocApp243Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/parent": {
"post": {
"tags": [
"hello-controller"
],
"operationId": "parentEndpoint",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/SuperClass"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {
"schemas": {
"Image": {
"required": [
"type"
],
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"Mail": {
"required": [
"type"
],
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"SuperClass": {
"required": [
"type"
],
"type": "object",
"properties": {
"type": {
"type": "string"
}
},
"discriminator": {
"propertyName": "type",
"mapping": {
"Image": "#/components/schemas/Image",
"Mail": "#/components/schemas/Mail"
}
},
"oneOf": [
{
"$ref": "#/components/schemas/Image"
},
{
"$ref": "#/components/schemas/Mail"
}
]
}
}
}
}