Swashbuckle.webapi: StackOverflow when using SchemaRegistry.GetOrRegister in ISchemaFiler.Apply

Created on 11 Mar 2016  Â·  8Comments  Â·  Source: domaindrivendev/Swashbuckle.WebApi

Hi,

I've got a StackOverflow while using a ISchemaFilter that need to register a new type. The recursion happens because I'm applying the filter to a referenced type, not a top-level type. During the while loop of GetOrRegister, my filter get's called for type A in which I call GetOrRegister for type B. This loops again on the 'not-yet-compiled' types and A it's still there ... and calls again my filter on the A type.

You can reproduce with an endpoint that takes this type as ([FromBody]Command c) signature.

public class Command 
{
    public int A {get ;set; }
    public Data Data {get;set;}
}

public class Data
{
    public int A {get;set;}
    public int B {get;set;}
    public IDictionary<object, object> ComplexFieldForWhichIWantToBuildSchema {get;set;}
}

public Filter : ISchemaFilter
{
    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
    {
        if (type == typeof(Data))
        {
            var t = typeof(KeyValuePair<string, string>[]);
            schema.properties["ComplexFieldForWhichIWantToBuildSchema"] = schemaRegistry.GetOrRegister(t);
        }
    }
}

All 8 comments

I can confirm that calling the private method SchemaRegistry.CreateInlineSchema using Reflection instead of SchemaRegistry.GetOrRegister solves the problem and gives the expected result.

I found the same problem. Also, the same problem exists for IOperationFilter and IModelFilter.

I just ran into this issue, doing exactly the same as OP. Is there a fix planned? Anyone know a way to workaround it?

I'm running into the exact same issue. Please let us know if there's a workaround / fix!

public static class IDontReallyWantToDoIt
{
    static MethodInfo _createInlineSchema = typeof(SchemaRegistry).GetMethod("CreateInlineSchema", BindingFlags.NonPublic | BindingFlags.Instance);
    public static Schema CreateInlineSchema(this SchemaRegistry @this, Type t)
    {
        return (Schema)_createInlineSchema.Invoke(@this, new object[] { t });
    }
}

Heh, IDontReallyWantToDoThat either. Turns out my idea for this was
pointless, I was going to try to manually inject OneOf into the schema, but
its not supported by Swagger Spec.
On Thu, Jul 21, 2016 at 10:20 AM AndreaCuneo [email protected]
wrote:

public static class IDontReallyWantToDoIt
{
static MethodInfo _createInlineSchema = typeof(SchemaRegistry).GetMethod("CreateInlineSchema", BindingFlags.NonPublic | BindingFlags.Instance);
public static Schema CreateInlineSchema(this SchemaRegistry @this, Type t)
{
return (Schema)_createInlineSchema.Invoke(@this, new object[] { t });
}
}

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/domaindrivendev/Swashbuckle/issues/680#issuecomment-234268415,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAek5faOQsSLkaDr3DKQaTklEAWpUfQOks5qX4A_gaJpZM4Huk6M
.

See PR #890 which resolves this. Will be available with the upcoming v5.5.0 release

I'm still seeing this issue in release 5.5.3. In my case I'm attempting to register a previously unregistered type, which might be different from the scenario in the test added in #890. Looks like it is attempting to re-run the SchemaFilters on the same type over & over which is causing the stack overflow exception.

Was this page helpful?
0 / 5 - 0 ratings