-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
523 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,87 @@ | ||
using System; | ||
using System.Linq; | ||
using Demo.Models.Products; | ||
using Demo.Models.Products; | ||
using Demo.Models.Wrapper; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Newtonsoft.Json; | ||
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Models; | ||
|
||
namespace Demo.Controllers { | ||
[Produces("application/json")] | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class ProductController : ControllerBase { | ||
[HttpPost] | ||
public IActionResult Post([FromForm] MultipartFormData<Product> data) { | ||
var json = data.Json ?? throw new NullReferenceException(nameof(data)); | ||
var image = data.File; | ||
return Ok(new { json, image?.FileName }); | ||
} | ||
namespace Demo.Controllers; | ||
|
||
[HttpPost("required")] | ||
public IActionResult Post([FromForm] MultipartRequiredFormData<Product> data) { | ||
var json = data.Json ?? throw new NullReferenceException(nameof(data)); | ||
var image = data.File; | ||
return Ok(new { json, image?.FileName }); | ||
} | ||
[Produces("application/json")] | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class ProductController : ControllerBase { | ||
[HttpPost] | ||
public IActionResult Post([FromForm] MultipartFormData<Product> data) { | ||
var json = data.Json ?? throw new NullReferenceException(nameof(data)); | ||
var image = data.File; | ||
return Ok(new { json, image?.FileName }); | ||
} | ||
|
||
[HttpPost("wrapper/required")] | ||
public IActionResult Post([FromForm] RequiredProductWrapper wrapper) { | ||
var wrapperProduct = wrapper.Product ?? throw new NullReferenceException(nameof(wrapper.Product)); | ||
var images = wrapper.Files; | ||
return Ok(new { wrapperProduct, images = images.Select(a => a.FileName) }); | ||
} | ||
[HttpPost("required")] | ||
public IActionResult Post([FromForm] MultipartRequiredFormData<Product> data) { | ||
var json = data.Json ?? throw new NullReferenceException(nameof(data)); | ||
var image = data.File; | ||
return Ok(new { json, image?.FileName }); | ||
} | ||
|
||
[HttpPost("required/nested")] | ||
public IActionResult Post([FromForm] MultipartRequiredFormData<NestedProduct> data) { | ||
var json = data.Json ?? throw new NullReferenceException(nameof(data)); | ||
var image = data.File; | ||
return Ok(new { json, image?.FileName }); | ||
} | ||
|
||
[HttpPost("wrapper")] | ||
public IActionResult PostWrapper([FromForm] ProductWrapper wrapper) { | ||
var wrapperProduct = wrapper.Product ?? throw new NullReferenceException(nameof(wrapper.Product)); | ||
var images = wrapper.Files; | ||
return Ok(new { wrapperProduct, images = images.Select(a => a.FileName) }); | ||
} | ||
[HttpPost("wrapper/required")] | ||
public IActionResult Post([FromForm] RequiredProductWrapper wrapper) { | ||
var wrapperProduct = wrapper.Product ?? throw new NullReferenceException(nameof(wrapper.Product)); | ||
var images = wrapper.Files; | ||
return Ok(new { wrapperProduct, images = images.Select(a => a.FileName) }); | ||
} | ||
|
||
[HttpPost("wrapper/simple")] | ||
public IActionResult PostWrapper([FromForm] SimpleProductWrapper wrapper) { | ||
var productName = wrapper.ProductName; | ||
var productId = wrapper.ProductId ?? throw new NullReferenceException(nameof(wrapper.ProductId)); | ||
var images = wrapper.Files; | ||
return Ok(new { productName, productId, images = images.Select(a => a.FileName) }); | ||
} | ||
[HttpPost("wrapper")] | ||
public IActionResult PostWrapper([FromForm] ProductWrapper wrapper) { | ||
var wrapperProduct = wrapper.Product ?? throw new NullReferenceException(nameof(wrapper.Product)); | ||
var images = wrapper.Files; | ||
return Ok(new { wrapperProduct, images = images.Select(a => a.FileName) }); | ||
} | ||
|
||
[HttpPost("wrapper/complex")] | ||
public IActionResult PostWrapper([FromForm] ComplexProductWrapper wrapper) { | ||
var productName = wrapper.ProductName; | ||
var productId = wrapper.ProductId ?? throw new NullReferenceException(nameof(wrapper.ProductId)); | ||
var product = wrapper.Product; | ||
var images = wrapper.Files; | ||
return Ok(new { | ||
productName, | ||
productId, | ||
product = JsonConvert.SerializeObject(product), | ||
images = images.Select(a => a.FileName) | ||
}); | ||
} | ||
[HttpPost("wrapper/simple")] | ||
public IActionResult PostWrapper([FromForm] SimpleProductWrapper wrapper) { | ||
var productName = wrapper.ProductName; | ||
var productId = wrapper.ProductId ?? throw new NullReferenceException(nameof(wrapper.ProductId)); | ||
var images = wrapper.Files; | ||
return Ok(new { productName, productId, images = images.Select(a => a.FileName) }); | ||
} | ||
|
||
[HttpPost("wrapper/complex-data")] | ||
public IActionResult PostDataWrapper([FromForm] ComplexProductWithDataWrapper wrapper) | ||
{ | ||
var productName = wrapper.ProductName; | ||
var productId = wrapper.ProductId ?? throw new NullReferenceException(nameof(wrapper.ProductId)); | ||
var product = wrapper.Product; | ||
var images = wrapper.Files; | ||
var data = wrapper.ProductData; | ||
return Ok(new | ||
{ | ||
productName, | ||
productId, | ||
product = JsonConvert.SerializeObject(product), | ||
images = images.Select(a => a.FileName), | ||
data = JsonConvert.SerializeObject(data) | ||
}); | ||
} | ||
} | ||
[HttpPost("wrapper/complex")] | ||
public IActionResult PostWrapper([FromForm] ComplexProductWrapper wrapper) { | ||
var productName = wrapper.ProductName; | ||
var productId = wrapper.ProductId ?? throw new NullReferenceException(nameof(wrapper.ProductId)); | ||
var product = wrapper.Product; | ||
var images = wrapper.Files; | ||
return Ok(new { | ||
productName, | ||
productId, | ||
product = JsonConvert.SerializeObject(product), | ||
images = images.Select(a => a.FileName) | ||
}); | ||
} | ||
|
||
[HttpPost("wrapper/complex-data")] | ||
public IActionResult PostDataWrapper([FromForm] ComplexProductWithDataWrapper wrapper) | ||
{ | ||
var productName = wrapper.ProductName; | ||
var productId = wrapper.ProductId ?? throw new NullReferenceException(nameof(wrapper.ProductId)); | ||
var product = wrapper.Product; | ||
var images = wrapper.Files; | ||
var data = wrapper.ProductData; | ||
return Ok(new | ||
{ | ||
productName, | ||
productId, | ||
product = JsonConvert.SerializeObject(product), | ||
images = images.Select(a => a.FileName), | ||
data = JsonConvert.SerializeObject(data) | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Swashbuckle.AspNetCore.JsonMultipartFormDataSupport\Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentValidation.AspNetCore" Version="11.0.2" /> | ||
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="5.6.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.0" /> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" /> | ||
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="6.0.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="7.0.0" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Swashbuckle.AspNetCore.JsonMultipartFormDataSupport\Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Demo.Models.Products; | ||
|
||
public sealed class NestedProduct | ||
{ | ||
public Guid Id { get; set; } | ||
public string? Name { get; set; } | ||
public ProductType Type { get; set; } | ||
public required NestedProductData Data { get; set; } | ||
} | ||
|
||
public sealed class NestedProductData | ||
{ | ||
public double Price { get; set; } | ||
public DateTime StartDate { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
using System; | ||
namespace Demo.Models.Products; | ||
|
||
namespace Demo.Models.Products { | ||
public class Product { | ||
public Guid Id { get; set; } | ||
public string? Name { get; set; } | ||
public ProductType Type { get; set; } | ||
} | ||
|
||
public enum ProductType { | ||
Phone, | ||
Laptop | ||
} | ||
public class Product { | ||
public Guid Id { get; set; } | ||
public string? Name { get; set; } | ||
public ProductType Type { get; set; } | ||
} | ||
|
||
public enum ProductType { | ||
Phone, | ||
Laptop | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
using System; | ||
namespace Demo.Models.Products; | ||
|
||
namespace Demo.Models.Products | ||
public class ProductData | ||
{ | ||
public class ProductData | ||
{ | ||
public double Price { get; set; } | ||
public DateTime StartDate { get; set; } | ||
} | ||
} | ||
public double Price { get; set; } | ||
public DateTime StartDate { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
using System; | ||
using Swashbuckle.AspNetCore.Filters; | ||
using Swashbuckle.AspNetCore.Filters; | ||
|
||
namespace Demo.Models.Products { | ||
public class ProductExamples : IExamplesProvider<Product> { | ||
public Product GetExamples() { | ||
var product = new Product { | ||
Id = Guid.NewGuid(), | ||
Name = "Example", | ||
Type = ProductType.Phone | ||
}; | ||
return product; | ||
} | ||
namespace Demo.Models.Products; | ||
|
||
public class ProductExamples : IExamplesProvider<Product> { | ||
public Product GetExamples() { | ||
var product = new Product { | ||
Id = Guid.NewGuid(), | ||
Name = "Example", | ||
Type = ProductType.Phone | ||
}; | ||
return product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
using Demo.Models.Products; | ||
using Microsoft.AspNetCore.Http; | ||
using System.ComponentModel.DataAnnotations; | ||
using Demo.Models.Products; | ||
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Attributes; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Demo.Models.Wrapper | ||
namespace Demo.Models.Wrapper; | ||
|
||
public class ComplexProductWithDataWrapper | ||
{ | ||
public class ComplexProductWithDataWrapper | ||
{ | ||
[FromJson] | ||
[Required] | ||
public Product Product { get; set; } = null!; | ||
[FromJson] | ||
[Required] | ||
public Product Product { get; set; } = null!; | ||
|
||
[FromJson] | ||
public ProductData? ProductData { get; set; } | ||
[FromJson] | ||
public ProductData? ProductData { get; set; } | ||
|
||
// [FromJson] <-- not required | ||
[Required] | ||
public int? ProductId { get; set; } | ||
// [FromJson] <-- not required | ||
[Required] | ||
public int? ProductId { get; set; } | ||
|
||
public string? ProductName { get; set; } | ||
public IFormFileCollection Files { get; set; } = new FormFileCollection(); | ||
} | ||
} | ||
public string? ProductName { get; set; } | ||
public IFormFileCollection Files { get; set; } = new FormFileCollection(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
using Demo.Models.Products; | ||
using Microsoft.AspNetCore.Http; | ||
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Attributes; | ||
|
||
namespace Demo.Models.Wrapper { | ||
public class ProductWrapper { | ||
[FromJson] // <-- This attribute is required for binding. | ||
public Product? Product { get; set; } | ||
namespace Demo.Models.Wrapper; | ||
|
||
public IFormFileCollection Files { get; set; } = new FormFileCollection(); | ||
} | ||
public class ProductWrapper { | ||
[FromJson] // <-- This attribute is required for binding. | ||
public Product? Product { get; set; } | ||
|
||
public IFormFileCollection Files { get; set; } = new FormFileCollection(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Demo.Models.Products; | ||
using Microsoft.AspNetCore.Http; | ||
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Attributes; | ||
|
||
namespace Demo.Models.Wrapper { | ||
public class RequiredProductWrapper { | ||
[Required] | ||
[FromJson] // <-- This attribute is required for binding. | ||
public Product Product { get; set; } = null!; | ||
namespace Demo.Models.Wrapper; | ||
|
||
public class RequiredProductWrapper { | ||
[Required] | ||
[FromJson] // <-- This attribute is required for binding. | ||
public Product Product { get; set; } = null!; | ||
|
||
[Required] | ||
public IFormFileCollection Files { get; set; } = new FormFileCollection(); | ||
} | ||
[Required] | ||
public IFormFileCollection Files { get; set; } = new FormFileCollection(); | ||
} |
Oops, something went wrong.