Skip to content

Commit 2496dc1

Browse files
author
Marcin Zarebski
committed
Use configured class replacement before extracting parameters from POJO
1 parent a924c00 commit 2496dc1

File tree

8 files changed

+252
-21
lines changed

8 files changed

+252
-21
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.swagger.v3.oas.models.parameters.RequestBody;
3232
import org.apache.commons.lang3.StringUtils;
3333
import org.springdoc.api.annotations.ParameterObject;
34+
import org.springdoc.core.converters.AdditionalModelsConverter;
3435
import org.springdoc.core.customizers.OperationCustomizer;
3536
import org.springdoc.core.customizers.ParameterCustomizer;
3637
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
@@ -144,7 +145,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
144145
for (int i = 0; i < parameters.length; ++i) {
145146
MethodParameter p = parameters[i];
146147
if (p.hasParameterAnnotation(ParameterObject.class)) {
147-
Class<?> paramClass = p.getParameterType();
148+
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
148149
Stream.of(paramClass.getDeclaredFields())
149150
.map(f -> DelegatingMethodParameter.fromGetterOfField(paramClass, f))
150151
.filter(Objects::nonNull)

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818

1919
package org.springdoc.core.converters;
2020

21-
22-
import java.util.HashMap;
23-
import java.util.Iterator;
24-
import java.util.Map;
25-
2621
import com.fasterxml.jackson.databind.JavaType;
2722
import io.swagger.v3.core.converter.AnnotatedType;
2823
import io.swagger.v3.core.converter.ModelConverter;
2924
import io.swagger.v3.core.converter.ModelConverterContext;
3025
import io.swagger.v3.core.util.Json;
3126
import io.swagger.v3.oas.models.media.Schema;
3227

28+
import java.util.HashMap;
29+
import java.util.Iterator;
30+
import java.util.Map;
31+
3332
public class AdditionalModelsConverter implements ModelConverter {
3433

3534
private static final Map<Class, Class> modelToClassMap = new HashMap();
@@ -44,15 +43,21 @@ public static void replaceWithSchema(Class source, Schema target) {
4443
modelToSchemaMap.put(source, target);
4544
}
4645

46+
public static Class getReplacement(Class clazz) {
47+
return modelToClassMap.getOrDefault(clazz, clazz);
48+
}
49+
4750
@Override
4851
public Schema resolve(AnnotatedType type, ModelConverterContext context, Iterator<ModelConverter> chain) {
4952
JavaType javaType = Json.mapper().constructType(type.getType());
5053
if (javaType != null) {
5154
Class<?> cls = javaType.getRawClass();
52-
if (modelToSchemaMap.containsKey(cls))
55+
if (modelToSchemaMap.containsKey(cls)) {
5356
return modelToSchemaMap.get(cls);
54-
if (modelToClassMap.containsKey(cls))
57+
}
58+
if (modelToClassMap.containsKey(cls)) {
5559
type = new AnnotatedType(modelToClassMap.get(cls)).resolveAsRef(true);
60+
}
5661
}
5762
return (chain.hasNext()) ? chain.next().resolve(type, context, chain) : null;
5863
}

springdoc-openapi-data-rest/src/main/java/org/springdoc/core/converters/Pageable.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,30 @@
1818

1919
package org.springdoc.core.converters;
2020

21-
import java.util.List;
22-
import java.util.Objects;
21+
import org.springframework.lang.Nullable;
2322

2423
import javax.validation.constraints.Max;
2524
import javax.validation.constraints.Min;
2625
import javax.validation.constraints.NotNull;
26+
import java.util.List;
27+
import java.util.Objects;
2728

2829
@NotNull
2930
public class Pageable {
3031

31-
@NotNull
32+
@Nullable
3233
@Min(0)
33-
private int page;
34+
private Integer page;
3435

35-
@NotNull
36+
@Nullable
3637
@Min(1)
3738
@Max(2000)
38-
private int size;
39+
private Integer size;
3940

40-
@NotNull
41+
@Nullable
4142
private List<String> sort;
4243

43-
public Pageable(@NotNull @Min(0) int page, @NotNull @Min(1) @Max(2000) int size, List<String> sort) {
44+
public Pageable(int page, int size, List<String> sort) {
4445
this.page = page;
4546
this.size = size;
4647
this.sort = sort;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.app7;
20+
21+
import org.springdoc.api.annotations.ParameterObject;
22+
import org.springframework.data.domain.Pageable;
23+
import org.springframework.http.ResponseEntity;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
import javax.validation.constraints.NotNull;
28+
import java.util.List;
29+
30+
@RestController
31+
public class HelloController {
32+
33+
@GetMapping(value = "/search", produces = { "application/xml", "application/json" })
34+
public ResponseEntity<List<PersonDTO>> getAllPets(@NotNull @ParameterObject Pageable pageable) {
35+
return null;
36+
}
37+
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.app7;
20+
21+
import io.swagger.v3.oas.annotations.media.Schema;
22+
23+
@Schema
24+
public class PersonDTO {
25+
private String email;
26+
27+
private String firstName;
28+
29+
private String lastName;
30+
31+
public PersonDTO() {
32+
}
33+
34+
public PersonDTO(final String email, final String firstName, final String lastName) {
35+
this.email = email;
36+
this.firstName = firstName;
37+
this.lastName = lastName;
38+
}
39+
40+
public String getEmail() {
41+
return email;
42+
}
43+
44+
public void setEmail(final String email) {
45+
this.email = email;
46+
}
47+
48+
public String getFirstName() {
49+
return firstName;
50+
}
51+
52+
public void setFirstName(final String firstName) {
53+
this.firstName = firstName;
54+
}
55+
56+
public String getLastName() {
57+
return lastName;
58+
}
59+
60+
public void setLastName(final String lastName) {
61+
this.lastName = lastName;
62+
}
63+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.app7;
20+
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
public class SpringDocApp7Test extends AbstractSpringDocTest {
25+
26+
@SpringBootApplication
27+
static class SpringDocTestApp {}
28+
29+
}

springdoc-openapi-data-rest/src/test/resources/results/app2.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@
5656
"components": {
5757
"schemas": {
5858
"Pageable": {
59-
"required": [
60-
"page",
61-
"size",
62-
"sort"
63-
],
6459
"type": "object",
6560
"properties": {
6661
"page": {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
"/search": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "getAllPets",
20+
"parameters": [
21+
{
22+
"name": "page",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"minimum": 0,
27+
"type": "integer",
28+
"format": "int32"
29+
}
30+
},
31+
{
32+
"name": "size",
33+
"in": "query",
34+
"required": false,
35+
"schema": {
36+
"default": 20,
37+
"maximum": 2000,
38+
"minimum": 1,
39+
"type": "integer",
40+
"format": "int32"
41+
}
42+
},
43+
{
44+
"name": "sort",
45+
"in": "query",
46+
"required": false,
47+
"schema": {
48+
"type": "array",
49+
"items": {
50+
"type": "string"
51+
}
52+
}
53+
}
54+
],
55+
"responses": {
56+
"200": {
57+
"description": "default response",
58+
"content": {
59+
"application/xml": {
60+
"schema": {
61+
"type": "array",
62+
"items": {
63+
"$ref": "#/components/schemas/PersonDTO"
64+
}
65+
}
66+
},
67+
"application/json": {
68+
"schema": {
69+
"type": "array",
70+
"items": {
71+
"$ref": "#/components/schemas/PersonDTO"
72+
}
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
80+
},
81+
"components": {
82+
"schemas": {
83+
"PersonDTO": {
84+
"type": "object",
85+
"properties": {
86+
"email": {
87+
"type": "string"
88+
},
89+
"firstName": {
90+
"type": "string"
91+
},
92+
"lastName": {
93+
"type": "string"
94+
}
95+
}
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)