Skip to content

Commit 0942572

Browse files
committed
add test to demonstrate
1 parent 50319e2 commit 0942572

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.Test;
6+
import org.springdoc.core.Constants;
7+
import org.springdoc.core.converters.ModelConverterRegistrar;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.test.web.servlet.MvcResult;
10+
import test.org.springdoc.api.AbstractSpringDocTest;
11+
12+
import java.util.List;
13+
14+
import static org.hamcrest.Matchers.*;
15+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
18+
19+
/**
20+
* This test is to make sure that a new model converter can access the parent of a type, even if
21+
* the type is enclosed in an ignored wrapper. We test this by setting up a model converter which
22+
* adds "stringy" to the "required" property of a schema's parent, when the sub schema is a String.
23+
*/
24+
public class SpringDocApp157Test extends AbstractSpringDocTest {
25+
26+
@SpringBootApplication
27+
static class SpringBootApp {}
28+
29+
@Test
30+
public void testApp() throws Exception {
31+
// Not sure why the converter isn't registered automatically. Register it here.
32+
new ModelConverterRegistrar(List.of(new StringyConverter()));
33+
mockMvc.perform(get(Constants.DEFAULT_API_DOCS_URL))
34+
.andExpect(status().isOk())
35+
.andExpect(jsonPath("$.openapi", is("3.0.1")))
36+
.andExpect(jsonPath("$.components.schemas.Foo.required", is(List.of("stringy"))))
37+
.andExpect(jsonPath("$.components.schemas.Bar", not(hasProperty("required"))));
38+
}
39+
}
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)