Skip to content

Commit 58f793b

Browse files
author
bnasslahsen
committed
Changes report: #1743
1 parent 67b1789 commit 58f793b

File tree

15 files changed

+588
-266
lines changed

15 files changed

+588
-266
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
import java.util.Collection;
3434
import java.util.Collections;
3535
import java.util.HashMap;
36+
import java.util.Iterator;
3637
import java.util.LinkedHashMap;
3738
import java.util.List;
3839
import java.util.Map;
40+
import java.util.Map.Entry;
3941
import java.util.Objects;
4042
import java.util.Optional;
4143
import java.util.Set;
@@ -263,6 +265,15 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
263265

264266
if (parameterDoc == null)
265267
parameterDoc = parametersDocMap.get(parameterInfo.getpName());
268+
269+
if (parameterDoc == null) {
270+
io.swagger.v3.oas.annotations.media.Schema schema = AnnotatedElementUtils.findMergedAnnotation(
271+
AnnotatedElementUtils.forAnnotations(methodParameter.getParameterAnnotations()), io.swagger.v3.oas.annotations.media.Schema.class);
272+
if (schema != null) {
273+
parameterDoc = parameterBuilder.generateParameterBySchema(schema);
274+
}
275+
}
276+
266277
// use documentation as reference
267278
if (parameterDoc != null) {
268279
if (parameterDoc.hidden() || parameterDoc.schema().hidden())
@@ -306,6 +317,28 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
306317
}
307318

308319
LinkedHashMap<String, Parameter> map = getParameterLinkedHashMap(components, methodAttributes, operationParameters, parametersDocMap);
320+
RequestBody requestBody = requestBodyInfo.getRequestBody();
321+
// support form-data
322+
if (requestBody != null
323+
&& requestBody.getContent() != null
324+
&& requestBody.getContent().containsKey(org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)) {
325+
io.swagger.v3.oas.models.media.Schema<?> mergedSchema = requestBodyInfo.getMergedSchema();
326+
Iterator<Entry<String, Parameter>> it = map.entrySet().iterator();
327+
while (it.hasNext()) {
328+
Entry<String, Parameter> entry = it.next();
329+
Parameter parameter = entry.getValue();
330+
if (!ParameterIn.PATH.toString().equals(parameter.getIn())) {
331+
io.swagger.v3.oas.models.media.Schema<?> itemSchema = new io.swagger.v3.oas.models.media.Schema() ;
332+
itemSchema.setName(entry.getKey());
333+
itemSchema.setDescription(parameter.getDescription());
334+
itemSchema.setDeprecated(parameter.getDeprecated());
335+
if (parameter.getExample() != null)
336+
itemSchema.setExample(parameter.getExample());
337+
mergedSchema.addProperty(entry.getKey(), itemSchema);
338+
it.remove();
339+
}
340+
}
341+
}
309342
setParams(operation, new ArrayList<>(map.values()), requestBodyInfo);
310343
return operation;
311344
}

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/GenericParameterService.java

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package org.springdoc.core.service;
2626

2727
import java.io.IOException;
28+
import java.lang.annotation.Annotation;
2829
import java.lang.reflect.ParameterizedType;
2930
import java.lang.reflect.Type;
3031
import java.lang.reflect.WildcardType;
@@ -41,6 +42,9 @@
4142
import io.swagger.v3.core.util.AnnotationsUtils;
4243
import io.swagger.v3.core.util.PrimitiveType;
4344
import io.swagger.v3.oas.annotations.enums.Explode;
45+
import io.swagger.v3.oas.annotations.enums.ParameterIn;
46+
import io.swagger.v3.oas.annotations.enums.ParameterStyle;
47+
import io.swagger.v3.oas.annotations.extensions.Extension;
4448
import io.swagger.v3.oas.annotations.media.ExampleObject;
4549
import io.swagger.v3.oas.models.Components;
4650
import io.swagger.v3.oas.models.examples.Example;
@@ -558,4 +562,103 @@ public Object resolveEmbeddedValuesAndExpressions(String value) {
558562
}
559563
return exprResolver.evaluate(placeholdersResolved, this.expressionContext);
560564
}
561-
}
565+
566+
/**
567+
* Generate parameter by schema
568+
*
569+
* @param schema the schema
570+
* @return the io.swagger.v3.oas.annotations.Parameter
571+
*/
572+
public io.swagger.v3.oas.annotations.Parameter generateParameterBySchema(io.swagger.v3.oas.annotations.media.Schema schema) {
573+
return new io.swagger.v3.oas.annotations.Parameter() {
574+
@Override
575+
public Class<? extends Annotation> annotationType() {
576+
return io.swagger.v3.oas.annotations.Parameter.class;
577+
}
578+
@Override
579+
public String name() {
580+
return schema.name();
581+
}
582+
583+
@Override
584+
public ParameterIn in() {
585+
return ParameterIn.DEFAULT;
586+
}
587+
588+
@Override
589+
public String description() {
590+
return schema.description();
591+
}
592+
593+
@Override
594+
public boolean required() {
595+
return schema.required();
596+
}
597+
598+
@Override
599+
public boolean deprecated() {
600+
return schema.deprecated();
601+
}
602+
603+
@Override
604+
public boolean allowEmptyValue() {
605+
return false;
606+
}
607+
608+
@Override
609+
public ParameterStyle style() {
610+
return ParameterStyle.DEFAULT;
611+
}
612+
613+
@Override
614+
public Explode explode() {
615+
return Explode.DEFAULT;
616+
}
617+
618+
@Override
619+
public boolean allowReserved() {
620+
return false;
621+
}
622+
623+
@Override
624+
public io.swagger.v3.oas.annotations.media.Schema schema() {
625+
return schema;
626+
}
627+
628+
@Override
629+
public io.swagger.v3.oas.annotations.media.ArraySchema array() {
630+
return null;
631+
}
632+
633+
@Override
634+
public io.swagger.v3.oas.annotations.media.Content[] content() {
635+
return new io.swagger.v3.oas.annotations.media.Content[0];
636+
}
637+
638+
@Override
639+
public boolean hidden() {
640+
return schema.hidden();
641+
}
642+
643+
@Override
644+
public ExampleObject[] examples() {
645+
return new ExampleObject[0];
646+
}
647+
648+
@Override
649+
public String example() {
650+
return schema.example();
651+
}
652+
653+
@Override
654+
public Extension[] extensions() {
655+
return schema.extensions();
656+
}
657+
658+
@Override
659+
public String ref() {
660+
return schema.ref();
661+
}
662+
};
663+
}
664+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test.org.springdoc.api.v30.app189;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
import io.swagger.v3.oas.annotations.tags.Tag;
7+
import org.springdoc.core.annotations.ParameterObject;
8+
9+
import org.springframework.http.MediaType;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.PostMapping;
12+
import org.springframework.web.bind.annotation.RequestPart;
13+
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.multipart.MultipartFile;
15+
16+
@Tag(name = "Article Api")
17+
@RestController("/article")
18+
public class ArticleApi {
19+
20+
@GetMapping("query")
21+
public List<ArticleDto> query(@ParameterObject ArticleQueryCondition condition) {
22+
return new LinkedList<>();
23+
}
24+
25+
@PostMapping(value = "create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
26+
public ArticleDto create(@ParameterObject ArticleDto dto, @RequestPart MultipartFile file) {
27+
return new ArticleDto();
28+
}
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.org.springdoc.api.v30.app189;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
@Schema
6+
public class ArticleDto {
7+
@Schema(description = "title")
8+
private String title;
9+
10+
@Schema(description = "content")
11+
private String content;
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(String title) {
18+
this.title = title;
19+
}
20+
21+
public String getContent() {
22+
return content;
23+
}
24+
25+
public void setContent(String content) {
26+
this.content = content;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package test.org.springdoc.api.v30.app189;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
@Schema
5+
public class ArticleQueryCondition {
6+
@Schema(description = "title")
7+
private String title;
8+
9+
public String getTitle() {
10+
return title;
11+
}
12+
13+
public void setTitle(String title) {
14+
this.title = title;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2022 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+
package test.org.springdoc.api.v30.app189;
24+
25+
import test.org.springdoc.api.v30.AbstractSpringDocV30Test;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
public class SpringDocApp189Test extends AbstractSpringDocV30Test {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}

0 commit comments

Comments
 (0)