Skip to content

Commit 26a99aa

Browse files
committed
Map Fields Disappear with Groovy on Classpath. Fixes #2046
1 parent 1af8c2f commit 26a99aa

File tree

8 files changed

+214
-14
lines changed

8 files changed

+214
-14
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/configuration/SpringDocGroovyConfiguration.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727

2828
import groovy.lang.MetaClass;
29-
import org.springdoc.core.converters.RequestTypeToIgnoreConverter;
29+
import org.springdoc.core.converters.JavaTypeToIgnoreConverter;
3030
import org.springdoc.core.providers.ObjectMapperProvider;
3131
import org.springdoc.core.utils.SpringDocUtils;
3232

@@ -58,7 +58,7 @@ public class SpringDocGroovyConfiguration {
5858
@Bean
5959
@Lazy(false)
6060
Object ignoreGroovyMetaClass() {
61-
SpringDocUtils.getConfig().addRequestWrapperToIgnore(MetaClass.class);
61+
SpringDocUtils.getConfig().addJavaTypeToIgnore(MetaClass.class);
6262
return null;
6363
}
6464

@@ -70,7 +70,7 @@ Object ignoreGroovyMetaClass() {
7070
*/
7171
@Bean
7272
@Lazy(false)
73-
RequestTypeToIgnoreConverter requestTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
74-
return new RequestTypeToIgnoreConverter(springDocObjectMapper);
73+
JavaTypeToIgnoreConverter requestTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
74+
return new JavaTypeToIgnoreConverter(springDocObjectMapper);
7575
}
7676
}

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

+37
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
/**
3838
* The type Converter utils.
39+
*
3940
* @author bnasslahsen
4041
*/
4142
public class ConverterUtils {
@@ -55,6 +56,11 @@ public class ConverterUtils {
5556
*/
5657
private static final List<Class<?>> FLUX_WRAPPERS_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
5758

59+
/**
60+
* The constant JAVA_TYPE_TO_IGNORE.
61+
*/
62+
private static final List<Class<?>> JAVA_TYPE_TO_IGNORE = Collections.synchronizedList(new ArrayList<>());
63+
5864
static {
5965
RESULT_WRAPPERS_TO_IGNORE.add(Callable.class);
6066
RESULT_WRAPPERS_TO_IGNORE.add(ResponseEntity.class);
@@ -157,4 +163,35 @@ public static void removeFluxWrapperToIgnore(Class<?> classes) {
157163
public static void addFluxWrapperToIgnore(Class<?> cls) {
158164
FLUX_WRAPPERS_TO_IGNORE.add(cls);
159165
}
166+
167+
/**
168+
* Add java type to ignore.
169+
*
170+
* @param cls the cls
171+
*/
172+
public static void addJavaTypeToIgnore(Class<?> cls) {
173+
JAVA_TYPE_TO_IGNORE.add(cls);
174+
}
175+
176+
/**
177+
* Remove java type to ignore.
178+
*
179+
* @param classes the classes
180+
*/
181+
public static void removeJavaTypeToIgnore(Class<?> classes) {
182+
List classesToIgnore = Arrays.asList(classes);
183+
if (JAVA_TYPE_TO_IGNORE.containsAll(classesToIgnore))
184+
JAVA_TYPE_TO_IGNORE.removeAll(Arrays.asList(classes));
185+
}
186+
187+
/**
188+
* Is java type to ignore boolean.
189+
*
190+
* @param rawClass the raw class
191+
* @return the boolean
192+
*/
193+
public static boolean isJavaTypeToIgnore(Class<?> rawClass) {
194+
return JAVA_TYPE_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
195+
}
196+
160197
}
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@
3232
import io.swagger.v3.core.converter.ModelConverterContext;
3333
import io.swagger.v3.oas.models.media.Schema;
3434
import org.springdoc.core.providers.ObjectMapperProvider;
35-
import org.springdoc.core.service.AbstractRequestService;
3635

3736
/**
3837
* The type Request type to ignore converter.
3938
* @author bnasslahsen
4039
*/
41-
public class RequestTypeToIgnoreConverter implements ModelConverter {
40+
public class JavaTypeToIgnoreConverter implements ModelConverter {
4241

4342
/**
4443
* The Spring doc object mapper.
@@ -50,7 +49,7 @@ public class RequestTypeToIgnoreConverter implements ModelConverter {
5049
*
5150
* @param springDocObjectMapper the spring doc object mapper
5251
*/
53-
public RequestTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
52+
public JavaTypeToIgnoreConverter(ObjectMapperProvider springDocObjectMapper) {
5453
this.springDocObjectMapper = springDocObjectMapper;
5554
}
5655

@@ -59,7 +58,7 @@ public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterato
5958
if (type.isSchemaProperty()) {
6059
JavaType javaType = springDocObjectMapper.jsonMapper().constructType(type.getType());
6160
Class<?> cls = javaType.getRawClass();
62-
if (AbstractRequestService.isRequestTypeToIgnore(cls))
61+
if (ConverterUtils.isJavaTypeToIgnore(cls))
6362
return null;
6463
}
6564
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;

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

+29-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
/**
4141
* The type Spring doc utils.
42+
*
4243
* @author bnasslahsen
4344
*/
4445
public class SpringDocUtils {
@@ -334,22 +335,44 @@ public SpringDocUtils setModelAndViewClass(Class<?> clazz) {
334335
/**
335336
* Remove from schema map spring doc utils.
336337
*
337-
* @param clazzs the clazzs
338+
* @param clazz the clazz
338339
* @return the spring doc utils
339340
*/
340-
public SpringDocUtils removeFromSchemaMap(Class<?> clazzs) {
341-
AdditionalModelsConverter.removeFromSchemaMap(clazzs);
341+
public SpringDocUtils removeFromSchemaMap(Class<?> clazz) {
342+
AdditionalModelsConverter.removeFromSchemaMap(clazz);
342343
return this;
343344
}
344345

345346
/**
346347
* Remove from schema class spring doc utils.
347348
*
348-
* @param clazzs the clazzs
349+
* @param clazz the clazz
350+
* @return the spring doc utils
351+
*/
352+
public SpringDocUtils removeFromSchemaClass(Class<?> clazz) {
353+
AdditionalModelsConverter.removeFromClassMap(clazz);
354+
return this;
355+
}
356+
357+
/**
358+
* Add java type to ignore spring doc utils.
359+
*
360+
* @param clazz the clazz
361+
* @return the spring doc utils
362+
*/
363+
public SpringDocUtils addJavaTypeToIgnore(Class<?> clazz) {
364+
ConverterUtils.addJavaTypeToIgnore(clazz);
365+
return this;
366+
}
367+
368+
/**
369+
* Remove java type to ignore spring doc utils.
370+
*
371+
* @param clazz the clazz
349372
* @return the spring doc utils
350373
*/
351-
public SpringDocUtils removeFromSchemaClass(Class<?> clazzs) {
352-
AdditionalModelsConverter.removeFromClassMap(clazzs);
374+
public SpringDocUtils removeJavaTypeToIgnore(Class<?> clazz) {
375+
ConverterUtils.removeJavaTypeToIgnore(clazz);
353376
return this;
354377
}
355378
}
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)