Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using generics in response causing invalid swagger.json #2661

Open
scphantm opened this issue Jun 2, 2023 · 6 comments
Open

Using generics in response causing invalid swagger.json #2661

scphantm opened this issue Jun 2, 2023 · 6 comments
Labels
help-wanted A change up for grabs for contributions from the community

Comments

@scphantm
Copy link

scphantm commented Jun 2, 2023

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?

@codelovercc
Copy link

Hi, try config SwaggerGenOptions this options.CustomSchemaIds(t=>KeyNameGeneratedByType(t)); when calling services.AddSwaggerGen. This may solve your issue.

@scphantm
Copy link
Author

Do you mind explaining that a bit more? I'm not sure how im supposed to reference my class in that.

@codelovercc
Copy link

$ref values must be RFC3986-compliant percent-encoded URIs

It says the Id is invalid, so let's custom the value of schemaIds to be a valid one.

@martincostello martincostello added the help-wanted A change up for grabs for contributions from the community label Apr 14, 2024
@cermakp
Copy link

cermakp commented Oct 2, 2024

Hi. any updates on that issue? It seems that either return type or properties in DTOs defined as generic have the same issue - meaning that generated name in openapi definition contains full assembly name as it is mentioned in the original post.

@jgarciadelanoceda
Copy link
Contributor

jgarciadelanoceda commented Oct 22, 2024

@scphantm the problem is that you are probably using the extensionMethod CustomSchemaIds when using SB.
SB by default prevents this behaviour see this code https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/231845f4ae24889b2f9bdad7fd6b57a8e1b2aa88/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGeneratorOptions.cs#L46C1-L55C10

I had this issue to, because by default we were doing:

options.CustomSchemaIds(x => x.FullName)

I had to change it to:

options.CustomSchemaIds(x =>
{
				var arguments = x.GetGenericArguments();
				if (arguments.Length > 0)
				{
					var result = $"{x.FullName.Split('`').First()}{arguments.Select(genericArg => genericArg.Name).Aggregate((previous, current) => previous + current)}";
					return result;
				}
				return x.FullName;
})

In case this solves your issue please close it

@jgarciadelanoceda
Copy link
Contributor

@martincostello I think the issue is related to the CustomsIds. There is no response from the author but SB will not do this behaviour by default.
So I think we can close it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help-wanted A change up for grabs for contributions from the community
Projects
None yet
Development

No branches or pull requests

5 participants