Skip to content

Commit 1de6335

Browse files
authored
Merge pull request #1153 from jagen31/resolve-inner-type
Modify ResponseSupportConverter to resolve inner type
2 parents d15bb20 + aaacec6 commit 1de6335

File tree

6 files changed

+152
-7
lines changed

6 files changed

+152
-7
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ResponseSupportConverter.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
4949
JavaType innerType = javaType.getBindings().getBoundType(0);
5050
if (innerType == null)
5151
return new StringSchema();
52-
else if (innerType.getBindings() != null && isResponseTypeWrapper(innerType.getRawClass())) {
53-
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).ctxAnnotations(type.getCtxAnnotations()).resolveAsRef(true);
54-
return this.resolve(type, context, chain);
55-
}
56-
else
57-
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).ctxAnnotations((type.getCtxAnnotations())).resolveAsRef(true);
52+
return context.resolve(new AnnotatedType(innerType)
53+
.jsonViewAnnotation(type.getJsonViewAnnotation())
54+
.ctxAnnotations((type.getCtxAnnotations()))
55+
.resolveAsRef(true));
5856
}
5957
else if (isResponseTypeToIgnore(cls))
6058
return null;
6159
}
6260
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
6361
}
6462

65-
}
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.app157;
2+
3+
/**
4+
* A class without a String in it
5+
*/
6+
public class Bar {
7+
private Object child;
8+
9+
public Object getChild() {
10+
return this.child;
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.api.app157;
2+
3+
/**
4+
* A class with a String in it
5+
*/
6+
public class Foo {
7+
private String child;
8+
9+
public String getChild() {
10+
return this.child;
11+
}
12+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 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.app157;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.ResponseEntity;
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
/**
27+
* Put Foo and Bar in the schema's components, make sure there is an ignored wrapper
28+
* ({@code ResponseEntity}).
29+
*/
30+
@RestController
31+
public class HelloController {
32+
33+
@GetMapping( "/foo")
34+
public ResponseEntity<Foo> getFoo() {
35+
return new ResponseEntity<Foo>(HttpStatus.OK);
36+
}
37+
38+
@GetMapping( "/bar")
39+
public ResponseEntity<Bar> getBar() {
40+
return new ResponseEntity<Bar>(HttpStatus.OK);
41+
}
42+
43+
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package test.org.springdoc.api.app157;
2+
3+
import io.swagger.v3.core.converter.ModelConverters;
4+
import io.swagger.v3.core.util.Json;
5+
import org.junit.jupiter.api.AfterAll;
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.springdoc.core.Constants;
11+
import org.springdoc.core.converters.ModelConverterRegistrar;
12+
import org.springframework.boot.autoconfigure.SpringBootApplication;
13+
import org.springframework.test.web.servlet.MvcResult;
14+
import test.org.springdoc.api.AbstractSpringDocTest;
15+
16+
import java.util.List;
17+
18+
import static org.hamcrest.Matchers.*;
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
21+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22+
23+
/**
24+
* This test is to make sure that a new model converter can access the parent of a type, even if
25+
* the type is enclosed in an ignored wrapper. We test this by setting up a model converter which
26+
* adds "stringy" to the "required" property of a schema's parent, when the sub schema is a String.
27+
*/
28+
public class SpringDocApp157Test extends AbstractSpringDocTest {
29+
30+
@SpringBootApplication
31+
static class SpringBootApp {}
32+
33+
private StringyConverter myConverter = new StringyConverter();
34+
private ModelConverters converters = ModelConverters.getInstance();
35+
36+
@BeforeEach
37+
public void registerConverter() {
38+
converters.addConverter(myConverter);
39+
}
40+
41+
@AfterEach
42+
public void unregisterConverter() {
43+
converters.removeConverter(myConverter);
44+
}
45+
46+
@Test
47+
public void testApp() throws Exception {
48+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL))
49+
.andExpect(status().isOk())
50+
.andExpect(jsonPath("$.openapi", is("3.0.1")))
51+
.andExpect(jsonPath("$.components.schemas.Foo.required", is(List.of("stringy"))))
52+
.andExpect(jsonPath("$.components.schemas.Bar", not(hasProperty("required"))));
53+
}
54+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test.org.springdoc.api.app157;
2+
3+
import com.fasterxml.jackson.databind.JavaType;
4+
import io.swagger.v3.core.converter.AnnotatedType;
5+
import io.swagger.v3.core.converter.ModelConverter;
6+
import io.swagger.v3.core.converter.ModelConverterContext;
7+
import io.swagger.v3.core.util.Json;
8+
import io.swagger.v3.oas.models.media.Schema;
9+
10+
import java.util.Iterator;
11+
12+
public class StringyConverter implements ModelConverter {
13+
14+
@Override
15+
public Schema resolve(AnnotatedType type, ModelConverterContext context,
16+
Iterator<ModelConverter> chain) {
17+
18+
JavaType javaType = Json.mapper().constructType(type.getType());
19+
20+
if (javaType.getRawClass().equals(String.class)) {
21+
type.getParent().addRequiredItem("stringy");
22+
}
23+
return chain.next().resolve(type, context, chain);
24+
}
25+
}

0 commit comments

Comments
 (0)