Skip to content

Commit

Permalink
@Schema oneOf config is ignored when generate the api-docs. Fixes #2705
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Oct 5, 2024
1 parent 3dba5e6 commit a98d3e6
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.swagger.v3.core.converter.ModelConverter;
import io.swagger.v3.core.converter.ModelConverterContext;
import io.swagger.v3.core.util.AnnotationsUtils;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
Expand Down Expand Up @@ -112,8 +113,16 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
type.resolveAsRef(true);
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
resolvedSchema = getResolvedSchema(javaType, resolvedSchema);
if (resolvedSchema == null || resolvedSchema.get$ref() == null)
if (resolvedSchema == null || resolvedSchema.get$ref() == null) {
return resolvedSchema;
}
if(resolvedSchema.get$ref().contains(Components.COMPONENTS_SCHEMAS_REF)) {
String schemaName = resolvedSchema.get$ref().substring(Components.COMPONENTS_SCHEMAS_REF.length());
Schema existingSchema = context.getDefinedModels().get(schemaName);
if (existingSchema != null && existingSchema.getOneOf() != null) {
return resolvedSchema;
}
}
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package test.org.springdoc.api.v30.app228;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author bnasslahsen
*/
@RestController
@RequestMapping
public class HelloController {


@PostMapping("swaggerTest")
public String swaggerTest(@RequestBody MyRequest myRequest) {
return null;
}

public class MyRequest {
@Schema(oneOf = {Child1.class, Child2.class})
@JsonProperty
private Parent parent;
}

@JsonSubTypes({
@JsonSubTypes.Type(value = Child1.class),
@JsonSubTypes.Type(value = Child2.class),
@JsonSubTypes.Type(value = Child3.class),
})
public abstract class Parent {
@JsonProperty
private String parentProperty;
}

public class Child1 extends Parent {
@JsonProperty
private String childProperty1;
}

public class Child2 extends Parent {
@JsonProperty
private String childProperty2;
}

public class Child3 extends Parent {
@JsonProperty
private String childProperty3;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* *
* * *
* * * *
* * * * * Copyright 2019-2022 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.app228;

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

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp228Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/swaggerTest": {
"post": {
"tags": [
"hello-controller"
],
"operationId": "swaggerTest",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/MyRequest"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "string"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Child1": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Parent"
},
{
"type": "object",
"properties": {
"childProperty1": {
"type": "string"
}
}
}
]
},
"Child2": {
"type": "object",
"allOf": [
{
"$ref": "#/components/schemas/Parent"
},
{
"type": "object",
"properties": {
"childProperty2": {
"type": "string"
}
}
}
]
},
"MyRequest": {
"type": "object",
"properties": {
"parent": {
"$ref": "#/components/schemas/Parent"
}
}
},
"Parent": {
"type": "object",
"properties": {
"parentProperty": {
"type": "string"
}
},
"oneOf": [
{
"$ref": "#/components/schemas/Child1"
},
{
"$ref": "#/components/schemas/Child2"
}
]
}
}
}
}

0 comments on commit a98d3e6

Please sign in to comment.