Search code examples
c#azure-functionsswagger-uiopenapi

Swagger UI with error "Could not resolve reference" from OpenAPI Azure Function


I'm creating an Azure Function where the request is of type 'Cliente', but when I view the Swagger UI, I get the following error:

enter image description here

These are the classes I'm using in the request:

public class CampoCliente
{
    [JsonProperty("nombre")]
    public string Nombre { get; set; }
}

public class CampoAtributo
{
    [JsonProperty("campos")]
    public string CampoAtri { get; set; }
}

public class OtroAtributo
{
    [JsonProperty("campos")]
    public CampoAtributo CampoXXX { get; set; }
}

public class Cliente
{
    [JsonProperty("campos")]
    public CampoCliente Campos { get; set; }

    [JsonProperty("attr")]
    public OtroAtributo Atributo { get; set; }
}

And this is my Azure Function

[OpenApiOperation("MyFunction", new[] { "Items" }, Description = "demo")]
[OpenApiRequestBody("application/json", typeof(Cliente), Required = true, Description = "demo")]
[Function(nameof(MyFunction))]
public HttpResponseData MyFunction([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req)
{
    _logger.LogInformation("C# HTTP trigger function processed a request.");
    var response = req.CreateResponse(HttpStatusCode.OK);
    response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
    response.WriteString("Welcome to Azure Functions!");
    return response;
}

I've been running some tests and I believe the problem is caused because the 'CampoXXX' property in the 'OtroAtributo' class has the same JsonProperty as the 'Campos' property in the 'Cliente' class. I made the change and the error disappeared by changing the JsonProperty of one of them, but unfortunately, I cannot do this because there is data that will arrive in that way.

Additionally, upon reviewing the generated file "http://localhost:7263/api/swagger.json", I have noticed that the definition for the "CampoAtributo" class has not been created.

Regarding this, is there any configuration I can do in the Azure Function or at a general level with OpenApi that can help me solve the problem?

Next, I'll show you my Program.cs class and the packages I'm using:

enter image description here


Solution

  • As per the error, the issue is caused by the JsonProperty attribute in the CampoXXX property in the OtroAtributo class having the same name as the Campos property in the Cliente class.

    You need to change the JsonProperty attribute of one of them to a different name.

    If that is not possible, you can use the OpenApiSchema attribute to define the schema for the OtroAtributo class and explicitly specify the property names.

    public class Cliente
    {
        public string Id { get; set; }
        public string Nombre { get; set; }
        public OtroAtributo OtroAtributo { get; set; }
    }
    
    public class OtroAtributo
    {
        [JsonProperty("campoXXX")]
        public string CampoXXX { get; set; }
        public string CampoYYY { get; set; }
    }
    
    [FunctionName("MyFunction")]
    [OpenApiOperation("MyFunction", new[] { "Items" }, Description = "demo")]
    [OpenApiRequestBody("application/json", typeof(Cliente), Required = true, Description = "demo")]
    public async Task<HttpResponseData> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestData req,
        FunctionContext executionContext)
    {
        var logger = executionContext.GetLogger("MyFunction");
        logger.LogInformation("C# HTTP trigger function processed a request.");
    
        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
        response.WriteString("Welcome to Azure Functions!");
    
        return response;
    }
    

    I used the below Nugets.

    enter image description here

    Console: enter image description here

    Swagger:

    enter image description here

    Json output
    

    enter image description here