Skip to content

Commit 38bc3b6

Browse files
author
bnasslahsen
committed
Merge branch 'michaldo-master'
2 parents 4dd6c9b + 6a3b6cf commit 38bc3b6

File tree

9 files changed

+260
-17
lines changed

9 files changed

+260
-17
lines changed

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.List;
3232
import java.util.Objects;
3333
import java.util.Optional;
34-
import java.util.stream.Stream;
3534

3635
import org.apache.commons.lang3.ArrayUtils;
3736
import org.springdoc.api.annotations.ParameterObject;
@@ -99,16 +98,10 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
9998
MethodParameter p = parameters[i];
10099
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
101100
if (p.hasParameterAnnotation(ParameterObject.class) || AnnotatedElementUtils.isAnnotated(paramClass, ParameterObject.class)) {
102-
Stream<MethodParameter> methodParameterStream = MethodParameterPojoExtractor.extractFrom(paramClass);
103-
if (!optionalDelegatingMethodParameterCustomizer.isPresent())
104-
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(explodedParameters::add);
105-
else {
106-
DelegatingMethodParameterCustomizer delegatingMethodParameterCustomizer = optionalDelegatingMethodParameterCustomizer.get();
107-
methodParameterStream.forEach(methodParameter -> {
108-
delegatingMethodParameterCustomizer.customize(p, methodParameter);
109-
explodedParameters.add(methodParameter);
110-
});
111-
}
101+
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
102+
optionalDelegatingMethodParameterCustomizer.ifPresent(customizer -> customizer.customize(p, methodParameter));
103+
explodedParameters.add(methodParameter);
104+
});
112105
}
113106
else {
114107
String name = pNames != null ? pNames[i] : p.getParameterName();
@@ -220,4 +213,4 @@ public int hashCode() {
220213
public boolean isParameterObject() {
221214
return isParameterObject;
222215
}
223-
}
216+
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static Optional<Content> getContent(io.swagger.v3.oas.annotations.media.C
199199
*/
200200
public static void mergeSchema(Content existingContent, Schema<?> schemaN, String mediaTypeStr) {
201201
if (existingContent.containsKey(mediaTypeStr)) {
202-
io.swagger.v3.oas.models.media.MediaType mediaType = existingContent.get(mediaTypeStr);
202+
MediaType mediaType = existingContent.get(mediaTypeStr);
203203
if (!schemaN.equals(mediaType.getSchema())) {
204204
// Merge the two schemas for the same mediaType
205205
Schema firstSchema = mediaType.getSchema();
@@ -221,7 +221,7 @@ public static void mergeSchema(Content existingContent, Schema<?> schemaN, Strin
221221
}
222222
else
223223
// Add the new schema for a different mediaType
224-
existingContent.addMediaType(mediaTypeStr, new io.swagger.v3.oas.models.media.MediaType().schema(schemaN));
224+
existingContent.addMediaType(mediaTypeStr, new MediaType().schema(schemaN));
225225
}
226226

227227
/**
@@ -232,9 +232,15 @@ public static void mergeSchema(Content existingContent, Schema<?> schemaN, Strin
232232
*/
233233
@SuppressWarnings("unchecked")
234234
public static boolean isAnnotationToIgnore(MethodParameter parameter) {
235-
return ANNOTATIONS_TO_IGNORE.stream().anyMatch(
236-
annotation -> parameter.getParameterAnnotation(annotation) != null
235+
boolean annotationFirstCheck = ANNOTATIONS_TO_IGNORE.stream().anyMatch(annotation ->
236+
(parameter.getParameterIndex() != -1 && AnnotationUtils.findAnnotation(parameter.getParameter(), annotation) != null)
237237
|| AnnotationUtils.findAnnotation(parameter.getParameterType(), annotation) != null);
238+
239+
boolean annotationSecondCheck = Arrays.stream(parameter.getParameterAnnotations()).anyMatch(annotation ->
240+
ANNOTATIONS_TO_IGNORE.contains(annotation.annotationType())
241+
|| ANNOTATIONS_TO_IGNORE.stream().anyMatch(annotationToIgnore -> annotation.annotationType().getDeclaredAnnotation(annotationToIgnore) != null));
242+
243+
return annotationFirstCheck || annotationSecondCheck;
238244
}
239245

240246
/**
@@ -246,7 +252,7 @@ public static boolean isAnnotationToIgnore(MethodParameter parameter) {
246252
public static boolean isAnnotationToIgnore(Type type) {
247253
return ANNOTATIONS_TO_IGNORE.stream().anyMatch(
248254
annotation -> (type instanceof Class
249-
&& AnnotationUtils.findAnnotation((Class<?>) type, annotation) != null));
255+
&& AnnotationUtils.findAnnotation((Class<?>) type, annotation) != null));
250256
}
251257

252258
/**
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 io.swagger.v3.oas.annotations.Hidden;
22+
23+
public class AnotherPerson {
24+
25+
@Hidden
26+
private String name;
27+
28+
public AnotherPerson() {
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test.org.springdoc.api.app7;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
10+
11+
@Target({ElementType.PARAMETER, ElementType.TYPE, ElementType.FIELD})
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Documented
14+
@AuthenticationPrincipal
15+
public @interface CurrentUser {
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
23+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
24+
import org.springframework.web.bind.annotation.PostMapping;
25+
import org.springframework.web.bind.annotation.RestController;
26+
27+
@RestController
28+
public class HelloController {
29+
30+
@PostMapping(value = "/hello")
31+
public String hello(
32+
@AuthenticationPrincipal Person person1,
33+
@CurrentUser Person person2,
34+
@ParameterObject AnotherPerson person3,
35+
@ParameterObject TheOtherPerson person4) {
36+
return "OK";
37+
}
38+
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
public class Person {
22+
23+
private String name;
24+
25+
public Person() {
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.models.OpenAPI;
22+
import io.swagger.v3.oas.models.info.Info;
23+
import io.swagger.v3.oas.models.info.License;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.context.annotation.Bean;
26+
import test.org.springdoc.api.AbstractSpringDocTest;
27+
28+
public class SpringDocApp7Test extends AbstractSpringDocTest {
29+
30+
@SpringBootApplication(scanBasePackages = { "test.org.springdoc.api.configuration,test.org.springdoc.api.app7" })
31+
static class SpringDocTestApp {
32+
@Bean
33+
public OpenAPI customOpenAPI() {
34+
return new OpenAPI()
35+
.info(new Info().title("Security API").version("v1")
36+
.license(new License().name("Apache 2.0").url("http://springdoc.org")));
37+
}
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
public class TheOtherPerson {
22+
23+
@CurrentUser
24+
private String name;
25+
26+
public TheOtherPerson() {
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public void setName(String name) {
34+
this.name = name;
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Security API",
5+
"license": {
6+
"name": "Apache 2.0",
7+
"url": "http://springdoc.org"
8+
},
9+
"version": "v1"
10+
},
11+
"servers": [
12+
{
13+
"url": "http://localhost",
14+
"description": "Generated server url"
15+
}
16+
],
17+
"paths": {
18+
"/hello": {
19+
"post": {
20+
"tags": [
21+
"hello-controller"
22+
],
23+
"operationId": "hello",
24+
"responses": {
25+
"200": {
26+
"description": "OK",
27+
"content": {
28+
"*/*": {
29+
"schema": {
30+
"type": "string"
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
},
39+
"components": {
40+
}
41+
}

0 commit comments

Comments
 (0)