Skip to content

Commit

Permalink
Query parameter part of request body in controller with MultiPartFile.
Browse files Browse the repository at this point in the history
…Fixes #1793
  • Loading branch information
bnasslahsen committed Aug 20, 2022
1 parent 3e8e84e commit 7ad1bb7
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ public abstract class AbstractRequestService {
*/
private final boolean defaultFlatParamObject;

/**
* The Default support form data.
*/
private boolean defaultSupportFormData;


/**
* Instantiates a new Abstract request builder.
*
Expand All @@ -191,6 +197,8 @@ protected AbstractRequestService(GenericParameterService parameterBuilder, Reque
this.parameterCustomizers = parameterCustomizers;
this.localSpringDocParameterNameDiscoverer = localSpringDocParameterNameDiscoverer;
this.defaultFlatParamObject = parameterBuilder.getPropertyResolverUtils().getSpringDocConfigProperties().isDefaultFlatParamObject();
this.defaultSupportFormData = parameterBuilder.getPropertyResolverUtils().getSpringDocConfigProperties().isDefaultSupportFormData();

}

/**
Expand Down Expand Up @@ -319,7 +327,7 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
LinkedHashMap<String, Parameter> map = getParameterLinkedHashMap(components, methodAttributes, operationParameters, parametersDocMap);
RequestBody requestBody = requestBodyInfo.getRequestBody();
// support form-data
if (requestBody != null
if (defaultSupportFormData && requestBody != null
&& requestBody.getContent() != null
&& requestBody.getContent().containsKey(org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)) {
io.swagger.v3.oas.models.media.Schema<?> mergedSchema = requestBodyInfo.getMergedSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public class SpringDocConfigProperties {
*/
private boolean defaultFlatParamObject;

/**
* convert query param to form data when consumes is multipart/form-data
*/
private boolean defaultSupportFormData=true;

/**
* The model Converters
*/
Expand All @@ -191,6 +196,24 @@ public class SpringDocConfigProperties {
*/
private SortConverter sortConverter = new SortConverter();

/**
* Is default support form data boolean.
*
* @return the boolean
*/
public boolean isDefaultSupportFormData() {
return defaultSupportFormData;
}

/**
* Sets default support form data.
*
* @param defaultSupportFormData the default support form data
*/
public void setDefaultSupportFormData(boolean defaultSupportFormData) {
this.defaultSupportFormData = defaultSupportFormData;
}

/**
* Gets sort converter.
*
Expand Down Expand Up @@ -821,7 +844,7 @@ public void setPolymorphicConverter(PolymorphicConverter polymorphicConverter) {

/**
* The type Sort converter.
* @author daniel-shuy
* @author daniel -shuy
*/
public static class SortConverter {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test.org.springdoc.api.v30.app194;

import java.util.LinkedList;
import java.util.List;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "Article Api")
@RestController("/article")
public class ArticleApi {

@GetMapping("query")
public List<ArticleDto> query(@ParameterObject ArticleQueryCondition condition) {
return new LinkedList<>();
}

@PostMapping(value = "create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ArticleDto create(@ParameterObject ArticleDto dto, @RequestPart MultipartFile file) {
return new ArticleDto();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package test.org.springdoc.api.v30.app194;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema
public class ArticleDto {
@Schema(description = "title")
private String title;

@Schema(description = "content")
private String content;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package test.org.springdoc.api.v30.app194;

import io.swagger.v3.oas.annotations.media.Schema;
@Schema
public class ArticleQueryCondition {
@Schema(description = "title")
private String title;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 the original author or authors.
* * * *
* * * * Licensed under the Apache License, Version 2.0 (the "License");
* * * * you may not use this file except in compliance with the License.
* * * * You may obtain a copy of the License at
* * * *
* * * * https://www.apache.org/licenses/LICENSE-2.0
* * * *
* * * * Unless required by applicable law or agreed to in writing, software
* * * * distributed under the License is distributed on an "AS IS" BASIS,
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * * * See the License for the specific language governing permissions and
* * * * limitations under the License.
* * *
* *
*
*/

package test.org.springdoc.api.v30.app194;

import test.org.springdoc.api.v30.AbstractSpringDocV30Test;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource(properties = "springdoc.default-support-form-data=false")
public class SpringDocApp194Test extends AbstractSpringDocV30Test {

@SpringBootApplication
static class SpringDocTestApp {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
}
],
"paths": {
"/create": {
"post": {
"tags": [
"Article Api"
],
"operationId": "create",
"parameters": [
{
"name": "title",
"in": "query",
"description": "title",
"required": false,
"schema": {
"type": "string",
"description": "title"
}
},
{
"name": "content",
"in": "query",
"description": "content",
"required": false,
"schema": {
"type": "string",
"description": "content"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"file"
],
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/ArticleDto"
}
}
}
}
}
}
},
"/query": {
"get": {
"tags": [
"Article Api"
],
"operationId": "query",
"parameters": [
{
"name": "title",
"in": "query",
"description": "title",
"required": false,
"schema": {
"type": "string",
"description": "title"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"*/*": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ArticleDto"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ArticleDto": {
"type": "object",
"properties": {
"title": {
"type": "string",
"description": "title"
},
"content": {
"type": "string",
"description": "content"
}
}
}
}
}
}

0 comments on commit 7ad1bb7

Please sign in to comment.