Stack Overflow when action has recursive url parameter
See original GitHub issueSwashbuckle.Swagger.Filters.HandleFromUriParams.ExtractAndAddQueryParams goes into an infinite recursive loop when you define an action with a url parameter that has circular references in it. IIS dies instantly with Stack Overflow Exception when Swashbuckle tries to generate the schema.
The problem only occurs when you explicitly set the model to be an url parameter with [FromUri] attribute. It can detect circular references correctly in body parameters.
Here is a small test case:
public class CategoryModel
{
public int Id { get; set; }
public CategoryModel Parent { get; set; }
}
public class CategoryController : ApiController
{
[HttpGet]
[Route("api/category/level")]
public int GetLevel([FromUri]CategoryModel model)
{
return 1;
}
}
And the stack trace:
...
Swashbuckle.Core.dll!Swashbuckle.Swagger.Filters.HandleFromUriParams.ExtractAndAddQueryParams(Swashbuckle.Swagger.Schema sourceSchema, string sourceQualifier, bool sourceRequired, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Collections.Generic.IList<Swashbuckle.Swagger.Parameter> operationParams) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.Filters.HandleFromUriParams.ExtractAndAddQueryParams(Swashbuckle.Swagger.Schema sourceSchema, string sourceQualifier, bool sourceRequired, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Collections.Generic.IList<Swashbuckle.Swagger.Parameter> operationParams) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.Filters.HandleFromUriParams.ExtractAndAddQueryParams(Swashbuckle.Swagger.Schema sourceSchema, string sourceQualifier, bool sourceRequired, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Collections.Generic.IList<Swashbuckle.Swagger.Parameter> operationParams) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.Filters.HandleFromUriParams.ExtractAndAddQueryParams(Swashbuckle.Swagger.Schema sourceSchema, string sourceQualifier, bool sourceRequired, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Collections.Generic.IList<Swashbuckle.Swagger.Parameter> operationParams) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.Filters.HandleFromUriParams.HandleFromUriObjectParams(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.Filters.HandleFromUriParams.Apply(Swashbuckle.Swagger.Operation operation, Swashbuckle.Swagger.SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(System.Web.Http.Description.ApiDescription apiDescription, Swashbuckle.Swagger.SchemaRegistry schemaRegistry) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(System.Collections.Generic.IEnumerable<System.Web.Http.Description.ApiDescription> apiDescriptions, Swashbuckle.Swagger.SchemaRegistry schemaRegistry) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.SwaggerGenerator.GetSwagger.AnonymousMethod__4(System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription> group) Unknown
System.Core.dll!System.Linq.Enumerable.ToDictionary<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>,string,Swashbuckle.Swagger.PathItem>(System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>> source, System.Func<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>,string> keySelector, System.Func<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>,Swashbuckle.Swagger.PathItem> elementSelector, System.Collections.Generic.IEqualityComparer<string> comparer) Unknown
System.Core.dll!System.Linq.Enumerable.ToDictionary<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>,string,Swashbuckle.Swagger.PathItem>(System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>> source, System.Func<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>,string> keySelector, System.Func<System.Linq.IGrouping<string,System.Web.Http.Description.ApiDescription>,Swashbuckle.Swagger.PathItem> elementSelector) Unknown
Swashbuckle.Core.dll!Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(string rootUrl, string apiVersion) Unknown
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Stack Overflow when action has recursive url parameter #262
ExtractAndAddQueryParams goes into an infinite recursive loop when you define an action with a url parameter that has circular references in it.
Read more >Stack Overflow when action has recursive url parameter
ExtractAndAddQueryParams goes into an infinite recursive loop when you define an action with a url parameter that has circular references in it.
Read more >When submitting a GET form, the query string is removed ...
When method is POST, and form is submitted, Query parameters in action url were intact (req.query) and input element data was sent as...
Read more >Good practice to pass this information through URL ...
Basically, let's suppose an application that manage some user's meetings. I have a filtering zone on a page that aims to specify the...
Read more >The StackOverflowError in Java
In the example shown below, a StackOverflowError will be thrown due to unintended recursion, where the developer has forgotten to specify a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@henkosch I just ran into this issue too although the recursion was due to using Entity Framework models in the parameters and as return types. Therefore, if you want to use any of those models in your parameters, Swagger will break. To me, this seems like a somewhat common use case as EF is used quite frequently.
I have the same issue, but I am quite sure, that the reason is just the circle reference itsself (not FromUri). A ‘small’ workaround is to map the ‘problem’ classes with: config.EnableSwagger(“docs/{apiVersion}/swagger”, c => { c.MapType<ProblemCircleClass>(() => new Schema { type = “ProblemCircleClass”, format = “ProblemCircleClass” }); … }
and repeat this line for every class with a circle in it.
(Note: this obviously won’t serialize the whole class, but you will get a json file in return and ‘just’ have to add all the problem classes by hand)