Azure / azure-functions-openapi-extension

This extension provides an Azure Functions app with Open API capability for better discoverability to consuming parties
https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.OpenApi/
MIT License
366 stars 190 forks source link

StackOverflow Exception on circular reference #54

Open Frankyfrankly opened 3 years ago

Frankyfrankly commented 3 years ago

Here is a code sample to reproduce the issue:

    public class Function1
    {
        private const string ContentType = "application/json";
        public List<Book> Lib { get; set; } = new List<Book>();

        [FunctionName(nameof(AddBook))]
        [OpenApiOperation(operationId: nameof(AddBook), Visibility = OpenApiVisibilityType.Important)]
        [OpenApiRequestBody(contentType: ContentType, bodyType: typeof(Book), Required = true)]
        [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: ContentType, bodyType: typeof(List<Book>))]
        public IActionResult AddBook([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)][FromBody] Book req, ILogger log)
        {
            Lib.Add(req);
            return new OkObjectResult(Lib);
        }
    }

    public class Book
    {
        public string Name { get; set; }
        public DateTime PublicationDate { get; set; }
        public Author Author { get; set; }
    }

    public class Author
    {
        public string Name { get; set; }
        public List<Book> Books { get; set; }
    }

It seems like recursion happens in Visit method of ObjectTypeVisitor class.

The same can be reproduced in Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.V3IoC project by adding a new class Owner:

       public class Owner
    {
        public string Name { get; set; }
        public List<Pet> Pets { get; set; }
    }

And introducing new property "public Owner Owner { get; set; }" in Pet class

justinyoo commented 3 years ago

@Frankyfrankly Thanks for the issue. I looked at the issue, but if you change the models' structure it will solve your issue:

public class Book
{
    ...
    public string AuthorId { get; set; }
}

public class Author
{
    ...
    public List<Book> Books { get; set; }
}

To me, the circular reference issue is about how data structure looks like, rather than the issue of this extension. If you still want to keep the Author in your Book class, I would recommend adding the [OpenApiIgnore()] decorator on top of it like:

public class Book
{
    ...
    public string AuthorId { get; set; }

    [OpenApiIgnore()]
    public Author Author { get; set; }
}

By doing so, you will avoid the circular reference issue.

justinyoo commented 3 years ago

@Frankyfrankly I'll close the issue for now. If you'd like to discuss more on this, please open a new issue.

justinyoo commented 3 years ago

Related to: https://github.com/aliencube/AzureFunctions.Extensions/issues/134

vlaskal commented 3 years ago

Hi @justinyoo ,

I run into same problem as is described https://github.com/aliencube/AzureFunctions.Extensions/issues/134 In my case I use health check library in Azure function project. And the response type HealthReport contains additional types including Exception type. I guess this is root cause of a bug and circular reference. As you see, these types cannot be changed as you suggest above. Also I attached call stack when exception is thrown image I am open to help in investigation of the issue.

john-patterson commented 3 years ago

...

    [OpenApiIgnore()]
    public Author Author { get; set; }
}

By doing so, you will avoid the circular reference issue.

This is not working for me as OpenApiIgnoreAttibute seems to only be valid for AttributeTargets.Method.

justinyoo commented 3 years ago

@john-patterson Oops my bad. I meant JsonIgnore.

john-patterson commented 3 years ago

@justinyoo ah. Okay, yeah I see in the code where that gets selected out. Unfortunately JsonIgnore isn't a great general solution as my other serializers handle my class just fine (my class Reviewer has an array of reviewers for whom they vote, based on the Azure DevOps type IdentityRefWithVote).

Is there opposition to a PR that generalizes OpenApiIgnore a bit to target properties and fields as well along with a patch to the RecursiveObjectTypeVisitor, ObjectTypeVisitor, and TypeExtensions? This should be backward compatible as JsonIgnore will still result in ignoring the attribute and OpenApiIgnore on methods will continue to behave the same.

If this works from a product management perspective, I can get a PR thrown together later today.

justinyoo commented 3 years ago

@vlaskal Thanks for your suggestion! What I'm suspecting where the StackOverflowException or CircularReferenceException occurs is these two parts:

https://github.com/Azure/azure-functions-openapi-extension/blob/626d2bd0c672d05114ac2ae775ca76393cdb3ee6/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ObjectTypeVisitor.cs#L208-L216

Adding the reference schemas should also be done before traversing each type's properties.

https://github.com/Azure/azure-functions-openapi-extension/blob/626d2bd0c672d05114ac2ae775ca76393cdb3ee6/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ObjectTypeVisitor.cs#L97-L109

It would be great if you can have a look. Once it's fixed, @john-patterson you wouldn't need to extend the OpenApiIgnore decorator.

vlaskal commented 3 years ago

@justinyoo Thanks for hints. I will try too look at it and will reach you back.

AndeyLapkoMobiDev commented 2 years ago

I have the same issue and a possible solution in #467

a-wallen commented 3 months ago

I'm also getting this error without any luck using the IgnoreJson attribute on circular references.