Skip to content

Commit

Permalink
Merge branch 'release/v.1.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Morasiu committed Oct 22, 2024
2 parents afde445 + 1d51eec commit 2f95acf
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 160 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.10.0] - 2024-10-22
### Added
- Support for multiple FromJson attributes in one wrapper class. Thanks @ArcKos00!

## [1.9.0] - 2023-12-17
### Added
- Support for DescribeAllParametersInCamelCase. Thanks @machekku!
Expand Down
28 changes: 23 additions & 5 deletions src/Demo/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ public IActionResult Post([FromForm] MultipartRequiredFormData<Product> data) {
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) });
return Ok(new { wrapperProduct, 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) });
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) });
return Ok(new { productName, productId, images = images.Select(a => a.FileName) });
}

[HttpPost("wrapper/complex")]
Expand All @@ -57,8 +57,26 @@ public IActionResult PostWrapper([FromForm] ComplexProductWrapper wrapper) {
productName,
productId,
product = JsonConvert.SerializeObject(product),
images = images?.Select(a => a.FileName)
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)
});
}
}
}
1 change: 1 addition & 0 deletions src/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Demo/Models/Products/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Demo.Models.Products {
public class Product {
public Guid Id { get; set; }
public string Name { get; set; }
public string? Name { get; set; }
public ProductType Type { get; set; }
}

Expand Down
10 changes: 10 additions & 0 deletions src/Demo/Models/Products/ProductData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Demo.Models.Products
{
public class ProductData
{
public double Price { get; set; }
public DateTime StartDate { get; set; }
}
}
24 changes: 24 additions & 0 deletions src/Demo/Models/Wrapper/ComplexProductWithDataWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Demo.Models.Products;
using Microsoft.AspNetCore.Http;
using Swashbuckle.AspNetCore.JsonMultipartFormDataSupport.Attributes;
using System.ComponentModel.DataAnnotations;

namespace Demo.Models.Wrapper
{
public class ComplexProductWithDataWrapper
{
[FromJson]
[Required]
public Product Product { get; set; } = null!;

[FromJson]
public ProductData? ProductData { get; set; }

// [FromJson] <-- not required
[Required]
public int? ProductId { get; set; }

public string? ProductName { get; set; }
public IFormFileCollection Files { get; set; } = new FormFileCollection();
}
}
8 changes: 4 additions & 4 deletions src/Demo/Models/Wrapper/ComplexProductWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace Demo.Models.Wrapper;
public class ComplexProductWrapper {
[FromJson]
[Required]
public Product Product { get; set; }
public Product Product { get; set; } = null!;

// [FromJson] <-- not required
[Required]
public int? ProductId { get; set; }

public string ProductName { get; set; }
public IFormFileCollection Files { get; set; }
public string? ProductName { get; set; }
public IFormFileCollection Files { get; set; } = new FormFileCollection();
}
4 changes: 2 additions & 2 deletions src/Demo/Models/Wrapper/ProductWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Demo.Models.Wrapper {
public class ProductWrapper {
[FromJson] // <-- This attribute is required for binding.
public Product Product { get; set; }
public Product? Product { get; set; }

public IFormFileCollection Files { get; set; }
public IFormFileCollection Files { get; set; } = new FormFileCollection();
}
}
4 changes: 2 additions & 2 deletions src/Demo/Models/Wrapper/RequiredProductWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Demo.Models.Wrapper {
public class RequiredProductWrapper {
[Required]
[FromJson] // <-- This attribute is required for binding.
public Product Product { get; set; }
public Product Product { get; set; } = null!;

[Required]
public IFormFileCollection Files { get; set; }
public IFormFileCollection Files { get; set; } = new FormFileCollection();
}
}
4 changes: 2 additions & 2 deletions src/Demo/Models/Wrapper/SimpleProductWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class SimpleProductWrapper {
[Required]
public int? ProductId { get; set; }

public string ProductName { get; set; }
public IFormFileCollection Files { get; set; }
public string? ProductName { get; set; }
public IFormFileCollection Files { get; set; } = new FormFileCollection();
}
}
Loading

0 comments on commit 2f95acf

Please sign in to comment.