7

I have a complex object to be passed as query string in ASP.Net Core 3.2.

public class Customer { public string Firstname { get; set; } public List<Address> Addresses { get; set; } } public class Address { public string Home_Address { get; set; } public string Office_Address { get; set; } } 

It works in Postman as below:

http://localhost:52100/v1/Customer?Addresses[0].Home_Address=123HomeStreet&Addresses[0].Office_Address=123OfficeStreet 

But, how to pass the value in Swagger documentation for Addresses which is of type "array[object] (query)" http://localhost:52100/v1/swagger-ui/index.html

I have Swashbuckle.AspNetCore 5.4.1, Swashbuckle.AspNetCore.Annotations 5.4.1, Swashbuckle.AspNetCore.Filters 5.1.1 references added in my project

9
  • Have you tried [FromQuery] attribute in your action? Commented Jun 16, 2020 at 12:07
  • Yes, I have used as public async Task<IActionResult> GetCustomer([FromQuery] Customer request) Commented Jun 16, 2020 at 12:37
  • Yes, I verified it does not provide fields in the Swagger UI when Array/List within model exists. Looks like you need to create custom OperationFilter for this. Commented Jun 16, 2020 at 13:12
  • can you provide some sample code on how to do this Commented Jun 16, 2020 at 13:46
  • OpenAPI Specification does not support arrays of objects in the query string - see stackoverflow.com/q/52892768/113116. Consider sending this parameter in the request body instead. Commented Jun 16, 2020 at 14:11

1 Answer 1

0

"Get" requests are not the industry standard for such queries. And here is a list with N adresses, so you should use the "Post" request with parameter attribute [FromBody]

[HttpPost] public async Task<ActionResult> PostAsync([FromBody] Customer customerObj, CancellationToken cancellationToken) { // your code } 

and Body:

{ "FirstName": "John", "Adresses": [ { "Address": { "Home_Address": "WorkDrive 11", "Office_Address": "HomeDrive 22" } } ] } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.