Circular references errror messaget in Azure OpenAPI function

Gupta, Varinder 0 Reputation points
2023-01-26T12:16:26.29+00:00

Hi I am using C# Azure OpenAPI function with return typeOf (customer), On Swagger.json validation online I am getting the below error message

Azure function::

   [FunctionName("Function1")]

    [OpenApiOperation(operationId: "Run", tags: new[] { "name" })]

    [OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Description = "The **Name** parameter")]

    [OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Customer), Description = "The OK response")]

    public async Task<IActionResult> Run(

        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)

    {

        _logger.LogInformation("C# HTTP trigger function processed a request.");


        string name = req.Query["name"];


        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

        dynamic data = JsonConvert.DeserializeObject(requestBody);

        name = name ?? data?.name;


        string responseMessage = string.IsNullOrEmpty(name)

            ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

            : $"Hello, {name}. This HTTP triggered function executed successfully.";


        return new OkObjectResult(responseMessage);

    }

}


public class Customer

{

    [OpenApiProperty]

    string code { set; get; }


    [OpenApiProperty]

    public Customer c { set; get; }

}

Error Message::

This API is valid, but it cannot be shown because it contains circular references

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    |     property 'properties' -> object with constructor 'Object'
    --- property 'c' closes the circle
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,940 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,506 Reputation points
    2023-02-03T05:12:43.1633333+00:00

    @Anonymous

    The Customer class has a public property "c" which seems to be a recursive reference to the same class. This may cause an infinite loop when serializing/deserializing JSON.
    To avoid an infinite loop scenario in the Customer class, you can remove the property "c" or change it to reference a different class that doesn't have a reference back to Customer.

    For example:

    public class Customer
    {
        [OpenApiProperty]
        public string code { set; get; }
    
        [OpenApiProperty]
        public AnotherClass anotherClass { set; get; }
    }
    
    public class AnotherClass
    {
        // Properties and methods here
    }
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.