Forum Discussion

scphantm's avatar
scphantm
New Contributor
11 months ago

Swagger generating invalid OpenApi3 json

I am trying to get our swagger schemas cleaned up so we can import them into an api management suite. My problem is swagger is generating invalid schemas. Take this definition

[HttpGet]
[ProducesResponseType(typeof(ListModel<CapacityConfigModel>), 200)]
public async Task<IActionResult> Get([FromQuery] CapacityQueryModel CapacityQueryModel)

 

This is valid because it works all over the place. My problem is when swagger gets to this in the swagger.json file, it generates json keys like this

"Myapp.Models.ListModel`1[[Myapp.Models.CapacityConfigModel, Myapp, Version=2023.5.16.1345, Culture=neutral, PublicKeyToken=null]]":

as the key definition, which creates references that look like this

"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Myapp.Models.ListModel`1[[Myapp.Models.CapacityConfigModel, CapacityConfigModel, Version=2023.5.16.1345, Culture=neutral, PublicKeyToken=null]]"
}
}
}
}
}

And my openAPI import and the swagger editor saying its wrong.  It fails due to that key doesn't pass the regex test. The only thing thats seems to be common with all of them is the ListModel class, and i don't see anything wrong with it.

public class ListModel<T>
{
/// <summary>
/// Gets the model type. This always returns <see cref="ModelType.List" />..
/// </summary>
public ModelType Model { get; } = ModelType.List;

/// <summary>
/// Gets or sets a continuation token that can be used to query the next page of results.
/// </summary>
public string ContinuationToken { get; set; }

/// <summary>
/// Gets or sets the actual list of results.
/// </summary>
public List<T> Data { get; set; } = new List<T>();
}

its pretty basic. Which leads me to believe swagger doesn't like generics very much. Can anyone else see where my problem is?

No RepliesBe the first to reply