swagger-api / swagger-api/swagger-codegen-generators
required of schema in multipart/form-data not supported
Open
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 299
- Forks
- 439
- PR merge metrics
- No merged PRs in 30d
Description
how to produce
using this openapi.json
{
"openapi": "3.0.1",
"info": {
"title": "OpenAPI definition",
"version": "v0"
},
"servers": [
{
"url": "http://localhost:8080",
"description": "Generated server url"
}
],
"tags": [
{
"name": "File",
"description": "upload"
}
],
"paths": {
"/files": {
"post": {
"tags": [
"File"
],
"summary": "upload single file",
"operationId": "uploadFile",
"parameters": [
{
"name": "bucket",
"in": "query",
"description": "bucket name",
"required": false,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"required": [
"file"
],
"type": "object",
"properties": {
"file": {
"type": "string",
"description": "file to upload",
"format": "binary"
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
will generate the wrong result
/**
* upload single file
* @param {Object} opts Optional parameters
* @param {Blob} opts.file
* @param {String} opts.bucket bucket name
* @return {Promise} a {@link https://www.promisejs.org/|Promise}
*/
uploadFile(opts) {
return this.uploadFileWithHttpInfo(file, opts)
.then(function(response_and_data) {
return response_and_data.data;
});
}
The correct result should be
/**
* upload single file
* @param {Blob} file
* @param {Object} opts Optional parameters
* @param {String} opts.bucket bucket name
* @return {Promise} a {@link https://www.promisejs.org/|Promise}
*/
uploadFile(file, opts) {
return this.uploadFileWithHttpInfo(file, opts)
.then(function(response_and_data) {
return response_and_data.data;
});
}
fix
Index: src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java b/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java
--- a/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java (revision 3ed70aeca12709c5b2929c6c4ae4cdc2a4ee2299)
+++ b/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java (revision 1c8f728b2f01ee0f23cdf042b14db87dd4241da2)
@@ -2178,13 +2178,14 @@
continue;
}
if (isForm) {
+ final List<String> schemaRequired = schema.getRequired();
final Map<String, Schema> propertyMap = schema.getProperties();
boolean isMultipart = contentType.equalsIgnoreCase("multipart/form-data");
if (propertyMap != null && !propertyMap.isEmpty()) {
for (String propertyName : propertyMap.keySet()) {
CodegenParameter formParameter = fromParameter(new Parameter()
.name(propertyName)
- .required(body.getRequired())
+ .required(body.getRequired() != null && body.getRequired() || schemaRequired.contains(propertyName))
.schema(propertyMap.get(propertyName)), imports);
if (isMultipart) {
formParameter.getVendorExtensions().put(CodegenConstants.IS_MULTIPART_EXT_NAME, Boolean.TRUE);
@@ -2192,7 +2193,7 @@
// todo: this segment is only to support the "older" template design. it should be removed once all templates are updated with the new {{#contents}} tag.
formParameter.getVendorExtensions().put(CodegenConstants.IS_FORM_PARAM_EXT_NAME, Boolean.TRUE);
operationParameters.addFormParam(formParameter.copy());
- if (body.getRequired() != null && body.getRequired()) {
+ if (formParameter.getRequired()) {
operationParameters.addRequiredParam(formParameter.copy());
}
operationParameters.addAllParams(formParameter);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java at the form-parameter handling, and compare the supplied multipart/form-data schema with the generated JavaScript signature. Done when the required file is emitted as a direct file argument while the optional bucket remains in opts, matching the expected output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, javascript
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100