Skip to content

Commit

Permalink
Merge branch '2801-infinite-recursion-with-all-of' of https://github.…
Browse files Browse the repository at this point in the history
…com/martinitus/springdoc-openapi into martinitus-2801-infinite-recursion-with-all-of
  • Loading branch information
bnasslahsen committed Jan 2, 2025
2 parents 353ffae + c4d2078 commit e1fff95
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* * * *
* * *
* *
*
*
*/

package org.springdoc.core.converters;
Expand Down Expand Up @@ -110,32 +110,35 @@ else if (resolvedSchema.getProperties().containsKey(javaType.getRawClass().getSi
@Override
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
if (javaType != null) {
for (Field field : FieldUtils.getAllFields(javaType.getRawClass())) {
if (field.isAnnotationPresent(JsonUnwrapped.class)) {
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
}

if (javaType == null || !chain.hasNext()) {
return null;
}

for (Field field : FieldUtils.getAllFields(javaType.getRawClass())) {
if (field.isAnnotationPresent(JsonUnwrapped.class)) {
PARENT_TYPES_TO_IGNORE.add(javaType.getRawClass().getSimpleName());
}
if (chain.hasNext()) {
if (!type.isResolveAsRef() && type.getParent() != null
&& PARENT_TYPES_TO_IGNORE.stream().noneMatch(ignore -> type.getParent().getName().startsWith(ignore)))
type.resolveAsRef(true);
Schema<?> resolvedSchema = chain.next().resolve(type, context, chain);
resolvedSchema = getResolvedSchema(javaType, resolvedSchema);
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());
}

String parentName = type.getParent() == null ? null : type.getParent().getName();
if (parentName != null && PARENT_TYPES_TO_IGNORE.stream().noneMatch(parentName::startsWith)){
type.resolveAsRef(true);
}

Schema<?> resolvedSchema = getResolvedSchema(javaType, chain.next().resolve(type, context, chain));
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 || existingSchema.getAllOf() != null || existingSchema.getAnyOf() != null)) {
return resolvedSchema;
}
}
return null;
return composePolymorphicSchema(type, resolvedSchema, context.getDefinedModels().values());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
*
* * Copyright 2019-2020 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.app13

import org.springdoc.core.properties.SpringDocConfigProperties
import org.springdoc.core.properties.SpringDocConfigProperties.ApiDocs.OpenApiVersion
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import test.org.springdoc.api.AbstractKotlinSpringDocMVCTest


@SpringBootTest//(classes = [Config::class])
class SpringDocApp13Test : AbstractKotlinSpringDocMVCTest() {


@Configuration
class Config{
@Bean
fun springDocConfigProperties():SpringDocConfigProperties{
val x= SpringDocConfigProperties()
x.apiDocs.version = OpenApiVersion.OPENAPI_3_1
return x
}

}


@SpringBootApplication
class DemoApplication

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* * Copyright 2019-2020 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.app13


import io.swagger.v3.oas.annotations.OpenAPI31
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

@Schema(description = "Generic description")
data class KeyValue(
val key: String,
val value: String,
)

@Schema
data class SomeDTO(
@Schema(description = "Description A") val fieldA: KeyValue,
@Schema(description = "Description B") val fieldB: KeyValue,
)



@RestController
@RequestMapping("/test")
class TestController {
@PostMapping("/test")
fun create(@RequestBody some: SomeDTO) {

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"openapi" : "3.1.0",
"info" : {
"title" : "OpenAPI definition",
"version" : "v0"
},
"servers" : [ {
"url" : "http://localhost",
"description" : "Generated server url"
} ],
"paths" : {
"/test/test" : {
"post" : {
"tags" : [ "test-controller" ],
"operationId" : "create",
"requestBody" : {
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/SomeDTO"
}
}
},
"required" : true
},
"responses" : {
"200" : {
"description" : "OK"
}
}
}
}
},
"components" : {
"schemas" : {
"KeyValue" : {
"required" : [ "key", "value" ],
"type" : "object",
"description" : "Generic description",
"properties" : {
"key" : {
"type" : "string"
},
"value" : {
"type" : "string"
}
}
},
"SomeDTO" : {
"required": [
"fieldA",
"fieldB"
],
"type": "object",
"properties": {
"fieldA": {
"description": "Description A",
"$ref": "#/components/schemas/KeyValue"
},
"fieldB": {
"description": "Description B",
"$ref": "#/components/schemas/KeyValue"
}
}
}
}
}
}

0 comments on commit e1fff95

Please sign in to comment.