Skip to content

Commit f27ff76

Browse files
committed
Map Fields Disappear with Groovy on Classpath. Fixes #2046
1 parent c29f36b commit f27ff76

File tree

8 files changed

+208
-7
lines changed

8 files changed

+208
-7
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/SpringDocUtils.java

+22
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,27 @@ public SpringDocUtils removeFromSchemaClass(Class<?> clazzs) {
346346
AdditionalModelsConverter.removeFromClassMap(clazzs);
347347
return this;
348348
}
349+
350+
/**
351+
* Add java type to ignore spring doc utils.
352+
*
353+
* @param clazz the clazz
354+
* @return the spring doc utils
355+
*/
356+
public SpringDocUtils addJavaTypeToIgnore(Class<?> clazz) {
357+
ConverterUtils.addJavaTypeToIgnore(clazz);
358+
return this;
359+
}
360+
361+
/**
362+
* Remove java type to ignore spring doc utils.
363+
*
364+
* @param clazz the clazz
365+
* @return the spring doc utils
366+
*/
367+
public SpringDocUtils removeJavaTypeToIgnore(Class<?> clazz) {
368+
ConverterUtils.removeJavaTypeToIgnore(clazz);
369+
return this;
370+
}
349371
}
350372

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

+38
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public class ConverterUtils {
5353
*/
5454
private static final List<Class<?>> FLUX_WRAPPERS_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
5555

56+
/**
57+
* The constant JAVA_TYPE_TO_IGNORE.
58+
*/
59+
private static final List<Class<?>> JAVA_TYPE_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
60+
61+
5662
static {
5763
RESULT_WRAPPERS_TO_IGNORE.add(Callable.class);
5864
RESULT_WRAPPERS_TO_IGNORE.add(ResponseEntity.class);
@@ -155,4 +161,36 @@ public static void removeFluxWrapperToIgnore(Class<?> classes) {
155161
public static void addFluxWrapperToIgnore(Class<?> cls) {
156162
FLUX_WRAPPERS_TO_IGNORE.add(cls);
157163
}
164+
165+
/**
166+
* Add java type to ignore.
167+
*
168+
* @param cls the cls
169+
*/
170+
public static void addJavaTypeToIgnore(Class<?> cls) {
171+
JAVA_TYPE_TO_IGNORE.add(cls);
172+
}
173+
174+
/**
175+
* Remove java type to ignore.
176+
*
177+
* @param classes the classes
178+
*/
179+
public static void removeJavaTypeToIgnore(Class<?> classes) {
180+
List classesToIgnore = Arrays.asList(classes);
181+
if (JAVA_TYPE_TO_IGNORE.containsAll(classesToIgnore))
182+
JAVA_TYPE_TO_IGNORE.removeAll(Arrays.asList(classes));
183+
}
184+
185+
/**
186+
* Is java type to ignore boolean.
187+
*
188+
* @param rawClass the raw class
189+
* @return the boolean
190+
*/
191+
public static boolean isJavaTypeToIgnore(Class<?> rawClass) {
192+
return JAVA_TYPE_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
193+
}
194+
195+
158196
}
+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* The type Request type to ignore converter.
3737
* @author bnasslahsen
3838
*/
39-
public class RequestTypeToIgnoreConverter implements ModelConverter {
39+
public class JavaTypeToIgnoreConverter implements ModelConverter {
4040

4141
/**
4242
* The Spring doc object mapper.
@@ -48,7 +48,7 @@ public class RequestTypeToIgnoreConverter implements ModelConverter {
4848
*
4949
* @param springDocObjectMapper the spring doc object mapper
5050
*/
51-
public RequestTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
51+
public JavaTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
5252
this.springDocObjectMapper = springDocObjectMapper;
5353
}
5454

@@ -57,7 +57,7 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
5757
if (type.isSchemaProperty()) {
5858
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
5959
Class<?> cls = javaType.getRawClass();
60-
if (AbstractRequestService.isRequestTypeToIgnore(cls))
60+
if (ConverterUtils.isJavaTypeToIgnore(cls))
6161
return null;
6262
}
6363
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;

springdoc-openapi-groovy/src/main/java/org/springdoc/groovy/SpringDocGroovyConfiguration.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import groovy.lang.MetaClass;
2626
import org.springdoc.core.SpringDocConfiguration;
2727
import org.springdoc.core.SpringDocUtils;
28-
import org.springdoc.core.converters.RequestTypeToIgnoreConverter;
28+
import org.springdoc.core.converters.JavaTypeToIgnoreConverter;
2929
import org.springdoc.core.providers.ObjectMapperProvider;
3030

3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -56,7 +56,7 @@ public class SpringDocGroovyConfiguration {
5656
@Bean
5757
@Lazy(false)
5858
Object ignoreGroovyMetaClass() {
59-
SpringDocUtils.getConfig().addRequestWrapperToIgnore(MetaClass.class);
59+
SpringDocUtils.getConfig().addJavaTypeToIgnore(MetaClass.class);
6060
return null;
6161
}
6262

@@ -68,7 +68,7 @@ Object ignoreGroovyMetaClass() {
6868
*/
6969
@Bean
7070
@Lazy(false)
71-
RequestTypeToIgnoreConverter requestTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
72-
return new RequestTypeToIgnoreConverter(springDocObjectMapper);
71+
JavaTypeToIgnoreConverter requestTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
72+
return new JavaTypeToIgnoreConverter(springDocObjectMapper);
7373
}
7474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.api.app11;
25+
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
import org.springframework.boot.autoconfigure.SpringBootApplication;
29+
30+
public class SpringDocApp11Test extends AbstractSpringDocTest {
31+
32+
@SpringBootApplication
33+
static class SpringDocTestApp {}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test.org.springdoc.api.app11;
2+
3+
/**
4+
* @author bnasslahsen
5+
*/
6+
7+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
8+
9+
import org.springframework.http.HttpStatus;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestBody;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.ResponseStatus;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
@RestController
17+
@RequestMapping("/test")
18+
public class TestController {
19+
@PostMapping
20+
@ApiResponse(responseCode = "200", description = "Random endpoint.")
21+
@ResponseStatus(HttpStatus.OK)
22+
public void testingMethod(@RequestBody TestRequest testRequest) {
23+
System.out.println("Method was run!");
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.api.app11;
2+
3+
/**
4+
* @author bnasslahsen
5+
*/
6+
7+
import java.util.Map;
8+
9+
import io.swagger.v3.oas.annotations.media.Schema;
10+
11+
public class TestRequest {
12+
@Schema(description = "Joe was here with a tuna melt!")
13+
private String joeWasHere;
14+
15+
@Schema(description = "This is an example of a map that does not work.!")
16+
private Map<String, String> testingTheMap;
17+
18+
public String getJoeWasHere() { return joeWasHere; }
19+
public void setJoeWasHere(String joeWasHere) { this.joeWasHere = joeWasHere; }
20+
public Map<String, String> getTestingTheMap() { return testingTheMap; }
21+
public void setTestingTheMap(Map<String, String> testingTheMap) { this.testingTheMap = testingTheMap; }
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/test": {
15+
"post": {
16+
"tags": [
17+
"test-controller"
18+
],
19+
"operationId": "testingMethod",
20+
"requestBody": {
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"$ref": "#/components/schemas/TestRequest"
25+
}
26+
}
27+
},
28+
"required": true
29+
},
30+
"responses": {
31+
"200": {
32+
"description": "Random endpoint."
33+
}
34+
}
35+
}
36+
}
37+
},
38+
"components": {
39+
"schemas": {
40+
"TestRequest": {
41+
"type": "object",
42+
"properties": {
43+
"joeWasHere": {
44+
"type": "string",
45+
"description": "Joe was here with a tuna melt!"
46+
},
47+
"testingTheMap": {
48+
"type": "object",
49+
"additionalProperties": {
50+
"type": "string",
51+
"description": "This is an example of a map that does not work.!"
52+
},
53+
"description": "This is an example of a map that does not work.!"
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)