diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java index 09460b3a7ff1..9a84686dfbac 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AspNetCoreServerCodegen.java @@ -31,6 +31,7 @@ import java.io.File; import java.net.URL; import java.util.Arrays; +import java.util.List; import java.util.Locale; import java.util.Map; @@ -398,6 +399,7 @@ public void processOpts() { } supportingFiles.add(new SupportingFile("Authentication" + File.separator + "ApiAuthentication.mustache", packageFolder + File.separator + "Authentication", "ApiAuthentication.cs")); + supportingFiles.add(new SupportingFile("Formatters" + File.separator + "InputFormatterStream.mustache", packageFolder + File.separator + "Formatters", "InputFormatterStream.cs")); } public void setPackageGuid(String packageGuid) { @@ -437,6 +439,68 @@ protected void processOperation(CodegenOperation operation) { operation.httpMethod = "Http" + operation.httpMethod.substring(0, 1) + operation.httpMethod.substring(1).toLowerCase(Locale.ROOT); } + @Override + public Map postProcessOperationsWithModels(Map objs, List allModels) { + super.postProcessOperationsWithModels(objs, allModels); + // We need to postprocess the operations to add proper consumes tags and fix form file handling + if (objs != null) { + Map operations = (Map) objs.get("operations"); + if (operations != null) { + List ops = (List) operations.get("operation"); + for (CodegenOperation operation : ops) { + if (operation.consumes == null) { + break; + } + if (operation.consumes.size() == 0) { + break; + } + + // Build a consumes string for the operation we cannot iterate in the template as we need a ',' + // after each entry but the last + + StringBuilder consumesString = new StringBuilder(); + for (Map consume : operation.consumes) { + if (!consume.containsKey("mediaType")) { + continue; + } + + if(consumesString.toString().equals("")) { + consumesString = new StringBuilder("\"" + consume.get("mediaType") + "\""); + } + else { + consumesString.append(", \"").append(consume.get("mediaType")).append("\""); + } + + // In a multipart/form-data consuming context binary data is best handled by an IFormFile + if (!consume.get("mediaType").equals("multipart/form-data")) { + continue; + } + + // Change dataType of binary parameters to IFormFile for formParams in multipart/form-data + for (CodegenParameter param : operation.formParams) { + if (param.isBinary) { + param.dataType = "IFormFile"; + param.baseType = "IFormFile"; + } + } + + for (CodegenParameter param : operation.allParams) { + if (param.isBinary && param.isFormParam) { + param.dataType = "IFormFile"; + param.baseType = "IFormFile"; + } + } + } + + if(!consumesString.toString().equals("")) { + operation.vendorExtensions.put("x-aspnetcore-consumes", consumesString.toString()); + } + } + } + } + return objs; + } + @Override public Mustache.Compiler processCompiler(Mustache.Compiler compiler) { // To avoid unexpected behaviors when options are passed programmatically such as { "useCollection": "" } diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Formatters/InputFormatterStream.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Formatters/InputFormatterStream.mustache new file mode 100644 index 000000000000..f1a002ded67c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Formatters/InputFormatterStream.mustache @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace {{packageName}}.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Startup.mustache index 2a4c1f53cf2a..044ce0c80e07 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Startup.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/Startup.mustache @@ -10,6 +10,7 @@ using Newtonsoft.Json.Serialization;{{#useSwashbuckle}} using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using {{packageName}}.Filters;{{/useSwashbuckle}} +using {{packageName}}.Formatters; namespace {{packageName}} { @@ -40,7 +41,9 @@ namespace {{packageName}} { // Add framework services. services - .AddMvc() + .AddMvc(options => { + options.InputFormatters.Insert(0, new InputFormatterStream()); + }) .AddJsonOptions(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/bodyParam.mustache index 02b0fa1d2dea..612860833043 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/bodyParam.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/bodyParam.mustache @@ -1 +1 @@ -{{#isBodyParam}}[FromBody]{{&dataType}} {{paramName}}{{/isBodyParam}} \ No newline at end of file +{{#isBodyParam}}{{^isBinary}}[FromBody]{{/isBinary}}{{&dataType}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/controller.mustache index 40fc9983db86..de15c470d1ed 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/controller.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/controller.mustache @@ -4,6 +4,7 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; {{#useSwashbuckle}} using Swashbuckle.AspNetCore.SwaggerGen; +using Microsoft.AspNetCore.Http; {{/useSwashbuckle}} using Newtonsoft.Json; using System.ComponentModel.DataAnnotations; @@ -26,6 +27,9 @@ namespace {{packageName}}.Controllers /// {{message}}{{/responses}} [{{httpMethod}}] [Route("{{{basePathWithoutHost}}}{{{path}}}")] + {{#vendorExtensions.x-aspnetcore-consumes}} + [Consumes({{&vendorExtensions.x-aspnetcore-consumes}})] + {{/vendorExtensions.x-aspnetcore-consumes}} [ValidateModelState]{{#useSwashbuckle}} [SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}} [SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/formParam.mustache index 80eb9f630bb9..7c9085c62dfe 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.0/formParam.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.0/formParam.mustache @@ -1 +1 @@ -{{#isFormParam}}[FromForm (Name = "{{baseName}}")]{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}}){{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file +{{#isFormParam}}{{^isBinary}}[FromForm (Name = "{{baseName}}")]{{/isBinary}}{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}})]{{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Formatters/InputFormatterStream.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Formatters/InputFormatterStream.mustache new file mode 100644 index 000000000000..f1a002ded67c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Formatters/InputFormatterStream.mustache @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace {{packageName}}.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache index d2f6997dd71e..50ad25a5e419 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/Startup.mustache @@ -13,6 +13,7 @@ using Swashbuckle.AspNetCore.Swagger; using Swashbuckle.AspNetCore.SwaggerGen; using {{packageName}}.Filters;{{/useSwashbuckle}} using {{packageName}}.Authentication; +using {{packageName}}.Formatters; using Microsoft.AspNetCore.Authorization; namespace {{packageName}} @@ -56,7 +57,10 @@ namespace {{packageName}} // Add framework services. services - .AddMvc({{^useDefaultRouting}}opts => opts.EnableEndpointRouting = false{{/useDefaultRouting}}) + .AddMvc(opts => { + {{^useDefaultRouting}}opts.EnableEndpointRouting = false;{{/useDefaultRouting}} + opts.InputFormatters.Insert(0, new InputFormatterStream()); + }) {{#compatibilityVersion}} .SetCompatibilityVersion(CompatibilityVersion.{{compatibilityVersion}}) {{/compatibilityVersion}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache index 02b0fa1d2dea..612860833043 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/bodyParam.mustache @@ -1 +1 @@ -{{#isBodyParam}}[FromBody]{{&dataType}} {{paramName}}{{/isBodyParam}} \ No newline at end of file +{{#isBodyParam}}{{^isBinary}}[FromBody]{{/isBinary}}{{&dataType}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache index 47e36aa25b6f..4cc411514c5e 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/controller.mustache @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Threading.Tasks; {{/operationResultTask}} using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; {{#useSwashbuckle}} using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; @@ -42,6 +43,9 @@ namespace {{apiPackage}} [Authorize{{#scopes}}{{#-first}}(Roles = "{{/-first}}{{scope}}{{^-last}},{{/-last}}{{#-last}}"){{/-last}}{{/scopes}}] {{/isBasicBearer}} {{/authMethods}} + {{#vendorExtensions.x-aspnetcore-consumes}} + [Consumes({{&vendorExtensions.x-aspnetcore-consumes}})] + {{/vendorExtensions.x-aspnetcore-consumes}} [ValidateModelState]{{#useSwashbuckle}} [SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}} [SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}}{{^useSwashbuckle}}{{#responses}}{{#dataType}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache index e71c9619d854..7c9085c62dfe 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/2.1/formParam.mustache @@ -1 +1 @@ -{{#isFormParam}}[FromForm (Name = "{{baseName}}")]{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}})]{{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file +{{#isFormParam}}{{^isBinary}}[FromForm (Name = "{{baseName}}")]{{/isBinary}}{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}})]{{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Formatters/InputFormatterStream.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Formatters/InputFormatterStream.mustache new file mode 100644 index 000000000000..f1a002ded67c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Formatters/InputFormatterStream.mustache @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace {{packageName}}.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Startup.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Startup.mustache index 6b8fd56ff9f3..250dcf665848 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Startup.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/Startup.mustache @@ -20,6 +20,7 @@ using {{packageName}}.Authentication; using {{packageName}}.Filters; {{/useSwashbuckle}} using {{packageName}}.OpenApi; +using {{packageName}}.Formatters; namespace {{packageName}} { @@ -65,7 +66,9 @@ namespace {{packageName}} // Add framework services. services // Don't need the full MVC stack for an API, see https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/ - .AddControllers() + .AddControllers(options => { + options.InputFormatters.Insert(0, new InputFormatterStream()); + }) {{#compatibilityVersion}} // Don't need this for 3.x - see https://docs.microsoft.com/en-us/aspnet/core/mvc/compatibility-version?view=aspnetcore-3.1 //.SetCompatibilityVersion(CompatibilityVersion.{{compatibilityVersion}}) diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/controller.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/controller.mustache index 73e6e4fa9ad7..0ff9c3644884 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/controller.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/controller.mustache @@ -6,6 +6,7 @@ using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; {{/operationResultTask}} using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; {{#useSwashbuckle}} using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; @@ -42,6 +43,9 @@ namespace {{apiPackage}} [Authorize{{#scopes}}{{#-first}}(Roles = "{{/-first}}{{scope}}{{^-last}},{{/-last}}{{#-last}}"){{/-last}}{{/scopes}}] {{/isBasicBearer}} {{/authMethods}} + {{#vendorExtensions.x-aspnetcore-consumes}} + [Consumes({{&vendorExtensions.x-aspnetcore-consumes}})] + {{/vendorExtensions.x-aspnetcore-consumes}} [ValidateModelState]{{#useSwashbuckle}} [SwaggerOperation("{{operationId}}")]{{#responses}}{{#dataType}} [SwaggerResponse(statusCode: {{code}}, type: typeof({{&dataType}}), description: "{{message}}")]{{/dataType}}{{^dataType}}{{/dataType}}{{/responses}}{{/useSwashbuckle}}{{^useSwashbuckle}}{{#responses}}{{#dataType}} diff --git a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/formParam.mustache b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/formParam.mustache index e71c9619d854..7c9085c62dfe 100644 --- a/modules/openapi-generator/src/main/resources/aspnetcore/3.0/formParam.mustache +++ b/modules/openapi-generator/src/main/resources/aspnetcore/3.0/formParam.mustache @@ -1 +1 @@ -{{#isFormParam}}[FromForm (Name = "{{baseName}}")]{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}})]{{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file +{{#isFormParam}}{{^isBinary}}[FromForm (Name = "{{baseName}}")]{{/isBinary}}{{#required}}[Required()]{{/required}}{{#pattern}}[RegularExpression("{{{pattern}}}")]{{/pattern}}{{#minLength}}{{#maxLength}}[StringLength({{maxLength}}, MinimumLength={{minLength}})]{{/maxLength}}{{/minLength}}{{#minLength}}{{^maxLength}} [MinLength({{minLength}})]{{/maxLength}}{{/minLength}}{{^minLength}}{{#maxLength}} [MaxLength({{maxLength}})]{{/maxLength}}{{/minLength}}{{#minimum}}{{#maximum}}[Range({{minimum}}, {{maximum}})]{{/maximum}}{{/minimum}}{{&dataType}} {{paramName}}{{/isFormParam}} \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES b/samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES index f39431847dff..25c2ed786eec 100644 --- a/samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES +++ b/samples/server/petstore/aspnetcore-3.0/.openapi-generator/FILES @@ -12,6 +12,7 @@ src/Org.OpenAPITools/Converters/CustomEnumConverter.cs src/Org.OpenAPITools/Dockerfile src/Org.OpenAPITools/Filters/BasePathFilter.cs src/Org.OpenAPITools/Filters/GeneratePathParamsValidationFilter.cs +src/Org.OpenAPITools/Formatters/InputFormatterStream.cs src/Org.OpenAPITools/Models/ApiResponse.cs src/Org.OpenAPITools/Models/Category.cs src/Org.OpenAPITools/Models/Order.cs diff --git a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/PetApi.cs b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/PetApi.cs index 12bfa96f1f1f..923ebb317d68 100644 --- a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/PetApi.cs +++ b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/PetApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; @@ -34,6 +35,7 @@ public class PetApiController : ControllerBase /// Invalid input [HttpPost] [Route("/v2/pet")] + [Consumes("application/json", "application/xml")] [ValidateModelState] [SwaggerOperation("AddPet")] public virtual IActionResult AddPet([FromBody]Pet body) @@ -214,7 +216,7 @@ public virtual IActionResult UpdatePetWithForm([FromRoute (Name = "petId")][Requ [ValidateModelState] [SwaggerOperation("UploadFile")] [SwaggerResponse(statusCode: 200, type: typeof(ApiResponse), description: "successful operation")] - public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, [FromForm (Name = "file")]System.IO.Stream file) + public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, System.IO.Stream file) { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... diff --git a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/StoreApi.cs b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/StoreApi.cs index 8d3101706af8..fca782c4b3b4 100644 --- a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/StoreApi.cs +++ b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/StoreApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; diff --git a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/UserApi.cs b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/UserApi.cs index 330997cd1af6..2047b00644ef 100644 --- a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/UserApi.cs +++ b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Controllers/UserApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; diff --git a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs new file mode 100644 index 000000000000..d810926aa9e7 --- /dev/null +++ b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace Org.OpenAPITools.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Startup.cs b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Startup.cs index 18b637c1a54c..70a6b10f0580 100644 --- a/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Startup.cs +++ b/samples/server/petstore/aspnetcore-3.0/src/Org.OpenAPITools/Startup.cs @@ -23,6 +23,7 @@ using Org.OpenAPITools.Authentication; using Org.OpenAPITools.Filters; using Org.OpenAPITools.OpenApi; +using Org.OpenAPITools.Formatters; namespace Org.OpenAPITools { @@ -64,7 +65,9 @@ public void ConfigureServices(IServiceCollection services) // Add framework services. services // Don't need the full MVC stack for an API, see https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/ - .AddControllers() + .AddControllers(options => { + options.InputFormatters.Insert(0, new InputFormatterStream()); + }) .AddNewtonsoftJson(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); diff --git a/samples/server/petstore/aspnetcore-3.1/.openapi-generator/FILES b/samples/server/petstore/aspnetcore-3.1/.openapi-generator/FILES index f39431847dff..25c2ed786eec 100644 --- a/samples/server/petstore/aspnetcore-3.1/.openapi-generator/FILES +++ b/samples/server/petstore/aspnetcore-3.1/.openapi-generator/FILES @@ -12,6 +12,7 @@ src/Org.OpenAPITools/Converters/CustomEnumConverter.cs src/Org.OpenAPITools/Dockerfile src/Org.OpenAPITools/Filters/BasePathFilter.cs src/Org.OpenAPITools/Filters/GeneratePathParamsValidationFilter.cs +src/Org.OpenAPITools/Formatters/InputFormatterStream.cs src/Org.OpenAPITools/Models/ApiResponse.cs src/Org.OpenAPITools/Models/Category.cs src/Org.OpenAPITools/Models/Order.cs diff --git a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/PetApi.cs b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/PetApi.cs index 12bfa96f1f1f..923ebb317d68 100644 --- a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/PetApi.cs +++ b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/PetApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; @@ -34,6 +35,7 @@ public class PetApiController : ControllerBase /// Invalid input [HttpPost] [Route("/v2/pet")] + [Consumes("application/json", "application/xml")] [ValidateModelState] [SwaggerOperation("AddPet")] public virtual IActionResult AddPet([FromBody]Pet body) @@ -214,7 +216,7 @@ public virtual IActionResult UpdatePetWithForm([FromRoute (Name = "petId")][Requ [ValidateModelState] [SwaggerOperation("UploadFile")] [SwaggerResponse(statusCode: 200, type: typeof(ApiResponse), description: "successful operation")] - public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, [FromForm (Name = "file")]System.IO.Stream file) + public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, System.IO.Stream file) { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... diff --git a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/StoreApi.cs b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/StoreApi.cs index 8d3101706af8..fca782c4b3b4 100644 --- a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/StoreApi.cs +++ b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/StoreApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; diff --git a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/UserApi.cs b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/UserApi.cs index 330997cd1af6..2047b00644ef 100644 --- a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/UserApi.cs +++ b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Controllers/UserApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; diff --git a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs new file mode 100644 index 000000000000..d810926aa9e7 --- /dev/null +++ b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace Org.OpenAPITools.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Startup.cs b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Startup.cs index 43b52c331bcd..d3f0be5bc677 100644 --- a/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Startup.cs +++ b/samples/server/petstore/aspnetcore-3.1/src/Org.OpenAPITools/Startup.cs @@ -23,6 +23,7 @@ using Org.OpenAPITools.Authentication; using Org.OpenAPITools.Filters; using Org.OpenAPITools.OpenApi; +using Org.OpenAPITools.Formatters; namespace Org.OpenAPITools { @@ -64,7 +65,9 @@ public void ConfigureServices(IServiceCollection services) // Add framework services. services // Don't need the full MVC stack for an API, see https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/ - .AddControllers() + .AddControllers(options => { + options.InputFormatters.Insert(0, new InputFormatterStream()); + }) .AddNewtonsoftJson(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); diff --git a/samples/server/petstore/aspnetcore-5.0/.openapi-generator/FILES b/samples/server/petstore/aspnetcore-5.0/.openapi-generator/FILES index f39431847dff..25c2ed786eec 100644 --- a/samples/server/petstore/aspnetcore-5.0/.openapi-generator/FILES +++ b/samples/server/petstore/aspnetcore-5.0/.openapi-generator/FILES @@ -12,6 +12,7 @@ src/Org.OpenAPITools/Converters/CustomEnumConverter.cs src/Org.OpenAPITools/Dockerfile src/Org.OpenAPITools/Filters/BasePathFilter.cs src/Org.OpenAPITools/Filters/GeneratePathParamsValidationFilter.cs +src/Org.OpenAPITools/Formatters/InputFormatterStream.cs src/Org.OpenAPITools/Models/ApiResponse.cs src/Org.OpenAPITools/Models/Category.cs src/Org.OpenAPITools/Models/Order.cs diff --git a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/PetApi.cs b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/PetApi.cs index 29f2b5560b91..ca7285f731c3 100644 --- a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/PetApi.cs +++ b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/PetApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; @@ -35,6 +36,7 @@ public class PetApiController : ControllerBase /// Invalid input [HttpPost] [Route("/v2/pet")] + [Consumes("application/json", "application/xml")] [ValidateModelState] [SwaggerOperation("AddPet")] [SwaggerResponse(statusCode: 200, type: typeof(Pet), description: "successful operation")] @@ -236,7 +238,7 @@ public virtual IActionResult UpdatePetWithForm([FromRoute (Name = "petId")][Requ [ValidateModelState] [SwaggerOperation("UploadFile")] [SwaggerResponse(statusCode: 200, type: typeof(ApiResponse), description: "successful operation")] - public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, [FromForm (Name = "file")]System.IO.Stream file) + public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, System.IO.Stream file) { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... diff --git a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/StoreApi.cs b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/StoreApi.cs index c92037c02bb2..08c813b8e139 100644 --- a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/StoreApi.cs +++ b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/StoreApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; diff --git a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/UserApi.cs b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/UserApi.cs index 2d0fa2261d75..b9436857201b 100644 --- a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/UserApi.cs +++ b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Controllers/UserApi.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Microsoft.AspNetCore.Authorization; using Swashbuckle.AspNetCore.SwaggerGen; @@ -36,6 +37,7 @@ public class UserApiController : ControllerBase [HttpPost] [Route("/v2/user")] [Authorize(Policy = "api_key")] + [Consumes("application/json")] [ValidateModelState] [SwaggerOperation("CreateUser")] public virtual IActionResult CreateUser([FromBody]User user) @@ -55,6 +57,7 @@ public virtual IActionResult CreateUser([FromBody]User user) [HttpPost] [Route("/v2/user/createWithArray")] [Authorize(Policy = "api_key")] + [Consumes("application/json")] [ValidateModelState] [SwaggerOperation("CreateUsersWithArrayInput")] public virtual IActionResult CreateUsersWithArrayInput([FromBody]List user) @@ -74,6 +77,7 @@ public virtual IActionResult CreateUsersWithArrayInput([FromBody]List user [HttpPost] [Route("/v2/user/createWithList")] [Authorize(Policy = "api_key")] + [Consumes("application/json")] [ValidateModelState] [SwaggerOperation("CreateUsersWithListInput")] public virtual IActionResult CreateUsersWithListInput([FromBody]List user) diff --git a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs new file mode 100644 index 000000000000..d810926aa9e7 --- /dev/null +++ b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace Org.OpenAPITools.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Startup.cs b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Startup.cs index 2d3b6e7fe70e..20cb5b5a9494 100644 --- a/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Startup.cs +++ b/samples/server/petstore/aspnetcore-5.0/src/Org.OpenAPITools/Startup.cs @@ -23,6 +23,7 @@ using Org.OpenAPITools.Authentication; using Org.OpenAPITools.Filters; using Org.OpenAPITools.OpenApi; +using Org.OpenAPITools.Formatters; namespace Org.OpenAPITools { @@ -64,7 +65,9 @@ public void ConfigureServices(IServiceCollection services) // Add framework services. services // Don't need the full MVC stack for an API, see https://andrewlock.net/comparing-startup-between-the-asp-net-core-3-templates/ - .AddControllers() + .AddControllers(options => { + options.InputFormatters.Insert(0, new InputFormatterStream()); + }) .AddNewtonsoftJson(opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); diff --git a/samples/server/petstore/aspnetcore/.openapi-generator/FILES b/samples/server/petstore/aspnetcore/.openapi-generator/FILES index 81b978877b1d..007264be5292 100644 --- a/samples/server/petstore/aspnetcore/.openapi-generator/FILES +++ b/samples/server/petstore/aspnetcore/.openapi-generator/FILES @@ -12,6 +12,7 @@ src/Org.OpenAPITools/Converters/CustomEnumConverter.cs src/Org.OpenAPITools/Dockerfile src/Org.OpenAPITools/Filters/BasePathFilter.cs src/Org.OpenAPITools/Filters/GeneratePathParamsValidationFilter.cs +src/Org.OpenAPITools/Formatters/InputFormatterStream.cs src/Org.OpenAPITools/Models/ApiResponse.cs src/Org.OpenAPITools/Models/Category.cs src/Org.OpenAPITools/Models/Order.cs diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs index a36bbafd5a98..cd49bccfe60b 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/PetApi.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; @@ -34,6 +35,7 @@ public class PetApiController : ControllerBase /// Invalid input [HttpPost] [Route("/v2/pet")] + [Consumes("application/json", "application/xml")] [ValidateModelState] [SwaggerOperation("AddPet")] public virtual IActionResult AddPet([FromBody]Pet body) @@ -214,7 +216,7 @@ public virtual IActionResult UpdatePetWithForm([FromRoute (Name = "petId")][Requ [ValidateModelState] [SwaggerOperation("UploadFile")] [SwaggerResponse(statusCode: 200, type: typeof(ApiResponse), description: "successful operation")] - public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, [FromForm (Name = "file")]System.IO.Stream file) + public virtual IActionResult UploadFile([FromRoute (Name = "petId")][Required]long petId, [FromForm (Name = "additionalMetadata")]string additionalMetadata, System.IO.Stream file) { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs index c552f329fe38..4503e177b9c9 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/StoreApi.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs index 5e2558125ae2..06ca8107d072 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Controllers/UserApi.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; using Swashbuckle.AspNetCore.Annotations; using Swashbuckle.AspNetCore.SwaggerGen; using Newtonsoft.Json; diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs new file mode 100644 index 000000000000..d810926aa9e7 --- /dev/null +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Formatters/InputFormatterStream.cs @@ -0,0 +1,32 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc.Formatters; + +namespace Org.OpenAPITools.Formatters +{ + // Input Type Formatter to allow model binding to Streams + public class InputFormatterStream : InputFormatter + { + public InputFormatterStream() + { + SupportedMediaTypes.Add("application/octet-stream"); + SupportedMediaTypes.Add("image/jpeg"); + } + + protected override bool CanReadType(Type type) + { + if (type == typeof(Stream)) + { + return true; + } + + return false; + } + + public override Task ReadRequestBodyAsync(InputFormatterContext context) + { + return InputFormatterResult.SuccessAsync(context.HttpContext.Request.Body); + } + } +} \ No newline at end of file diff --git a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs index 8afc90fa56c6..8c5a715fb713 100644 --- a/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs +++ b/samples/server/petstore/aspnetcore/src/Org.OpenAPITools/Startup.cs @@ -22,6 +22,7 @@ using Swashbuckle.AspNetCore.SwaggerGen; using Org.OpenAPITools.Filters; using Org.OpenAPITools.Authentication; +using Org.OpenAPITools.Formatters; using Microsoft.AspNetCore.Authorization; namespace Org.OpenAPITools @@ -61,7 +62,10 @@ public void ConfigureServices(IServiceCollection services) // Add framework services. services - .AddMvc() + .AddMvc(opts => { + + opts.InputFormatters.Insert(0, new InputFormatterStream()); + }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) .AddJsonOptions(opts => { diff --git a/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestHeadersApi.java b/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestHeadersApi.java index 6aea0da1bf2d..2e9994a9f744 100644 --- a/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestHeadersApi.java +++ b/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestHeadersApi.java @@ -12,7 +12,6 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; -import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.multipart.MultipartFile; diff --git a/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestQueryParamsApi.java b/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestQueryParamsApi.java index 337027e4cb7a..dd696891f1b2 100644 --- a/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestQueryParamsApi.java +++ b/samples/server/petstore/spring-mvc-default-value/src/main/java/org/openapitools/api/TestQueryParamsApi.java @@ -12,7 +12,6 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; -import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.multipart.MultipartFile;