vaclavnovotny / NSwag.Examples

NSwag processor to programmatically define strongly-typed examples for response and request parameters.
MIT License
13 stars 11 forks source link

Providing examples of simple types #5

Closed davidkeaveny closed 2 years ago

davidkeaveny commented 3 years ago

I have some API endpoints that return string arrays; these obviously won't work with the NSwag.Examples provider model, which is looking for complex types. Do you have any thoughts/suggestions on how to provide (possibly inline) example values?

I was hoping that the <example> tag would be picked up and parsed as a JSON object, but that doesn't seem to happen when NSwag.Examples is present e.g.:

/// <example>[ \"value1\", \"value2\", \"value3\" ]</example>
[ProducesResponseType(typeof(string[]), StatusCodes.Status200OK)]
[HttpGet("/string-values")]
public IActionResult GetStringValues()
{
  return Ok(new[] { "value1", "value2", "value3" });
}

I could write my own operation processor easily enough, but I was hoping there might be something more obvious that I might have overlooked?

vaclavnovotny commented 3 years ago

Hi @davidkeaveny, There is support for collections of primitive types, however, you will not be able to distinguish examples for multiple methods with this return type. I think we should add support to be able to define controller+method specific example provider. What do you think? Would that solve your problem?

davidkeaveny commented 3 years ago

Yes, I think that that's what I was heading towards when I said I could write an operation processor. Although I still like the idea of parsing the <example> tags, which would reduce the overall amount of code needed.

vaclavnovotny commented 3 years ago

Yeah, I think both approaches will be useful. However, we should limit the example tag to only collections of primitive types. Are you able to create PR by yourself? Or do you want me to implement it?

vaclavnovotny commented 2 years ago

Closing due to inactivity. Please reopen when needed.