Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

Using generics in response causing invalid swagger.json

Open scphantm opened this issue 1 year ago • 6 comments

we are using the latest version of swashbuckle.

I have this class

 public class ListModel<T>
    {
        public ModelType Model { get; } = ModelType.List;
        public string ContinuationToken { get; set; }
        public List<T> Data { get; set; } = new List<T>();
    }

We use this class all over the place in our 200 responses back to the javascript front end. We never had a problem before, everything on the application side works perfectly, has for a long time and still works. But, we are in the process of importing our services into an APIM and realized for the first time that for some reason, that class causes json key names to be invalid.

Whenever we put that class into our http definition like this

        [HttpGet("VendorChangeTracking")]
        [ProducesResponseType(typeof(ListModel<VendorReportModel>), 200)]
        public async Task<IActionResult> GetFunStuffTracking(TrackingQueryModel trackingQueryModel)

When swagger translates the ListModel<VendorReportModel> in the json to the schema, it generates a key name that looks like this

"bla.bla.ListModel'1[[bla.bla.VendorReportModel, bla.CommonVendorProfileLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"

which generates the error message

Structural error at paths./api/v1/Foo/Bar.get.responses.200.content.application/json.schema.$ref
should match format "uri-reference"
format: uri-reference
Jump to line 186

Semantic error at paths./api/v1/Foo/Bar.get.responses.200.content.application/json.schema.$ref
$ref values must be RFC3986-compliant percent-encoded URIs
Jump to line 186

we tried to drop the annotation, but, if the method is defined like this

        [HttpGet]
        [ServiceFilter(typeof(TransactionRequiredAttribute))]
        public async Task<ActionResult<ListModel<NoteModel>>> GetAsync([FromQuery]NoteQueryModel query)

The response is the same. we have been working on this for days now and got nothing. Anyone have any ideas for me here?

scphantm avatar Jun 02 '23 16:06 scphantm