Nswag: Complex type as nested object not generating using nSwag in swagger post api request

Created on 7 Mar 2020  ·  10Comments  ·  Source: RicoSuter/NSwag

Hi,
I'm using swagger open api , and nswag to generate proper request / response in swagger documentation in .Net core 3.0 project. Everything works fine with type string, int, boolean and for array too. But I have one property in POST api request as Object (of my custom model). That is not being generated. If I change to array then it works, but I require single object only.
Can any one help me out? I'm in hurry.

Most helpful comment

The inclusion of the following line:
     config.AllowReferencesWithProperties = true;
In my swagger configuration, it solved my problem.

...
services.AddOpenApiDocument(config =>
    {                    config.AddSecurity("apikey", Enumerable.Empty<string>(), new OpenApiSecurityScheme
        {      
            Type = OpenApiSecuritySchemeType.ApiKey,
            Name = "token",
            In = OpenApiSecurityApiKeyLocation.Header,
            Description = "Forneça o token para acessar as funções operacionais da API: Bearer {token}"
        });

        config.OperationProcessors.Add(
            new AspNetCoreOperationSecurityScopeProcessor("bearer"));
        config.Version = "v1";
        config.AllowReferencesWithProperties = true;  /*<<<<<<<<<< HERE*/
        config.Title = "Microsum api.mstiDFE - Documentação";
        config.Description = "" +
            "**API para geração de Documentos Fiscais Eletrônicos (DF-e) do projeto SPED**</br>" +
            "*Subtítulo*</br>" +
            "Token operacional: `ff29894a7a5c7be56a0ac2bfbb4f7b44bbe07ec3`" +
            "" +
            "" +
            "";
    });
...

All 10 comments

Can you provide sample code and the generated spec?

Can you provide sample code and the generated spec?

public class ParentDemo
{
public string P1{get;set;}

public string p2{get;set;}

public ChildDemo c {get;set;}

}

public class ChildDemo
{
public int ID{get;set;}
public string Name{get;set;}
}

But in Example value schema :
{
"P1": "string",
"p2": "string",
}

Strange, is this a regular [FromBody] parameter?

Yes @RicoSuter , I'm using .Net core 3.0
And yes strange thing, it is not generated in Example value but it is there in schema

Can you post the spec? It looks like a swagger ui problem

@RicoSuter : I'm not getting your point.. spec?

Spec = openapi schema

I have the same problem here:

Microsoft.AspNetCore.App: 3.1.0-preview2.19528.8
NSwag.AspNetCore: 13.3.0

public class NFSeController : DefaultControllerDFe
{
    //private readonly INFSeServices _iNFSeServices;

    //public NFSeController(INFSeServices NFSeServices)
    //{
    //    _iNFSeServices = NFSeServices;
    //}

    [HttpPost]
    [Route("api/nfse/")]
    public async Task<IResultVM> Incluir([FromBody] ClasseTeste DadosNFSe)
    {
        return null;
    }
}

public class ClasseTeste
{
    public string Propriedade1 { get; set; }
    public string Propriedade2 { get; set; }
    public int Propriedade3 { get; set; }
    public ClasseTeste2 PropriedadeComplexa { get; set; }
}

public class ClasseTeste2
{
    public string Propriedade1 { get; set; }
    public string Propriedade2 { get; set; }
    public int Propriedade3 { get; set; }
}

Problema no swagger

Spec:

  "ClasseTeste": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "Propriedade1": {
        "type": "string",
        "nullable": true
      },
      "Propriedade2": {
        "type": "string",
        "nullable": true
      },
      "Propriedade3": {
        "type": "integer",
        "format": "int32"
      },
      "PropriedadeComplexa": {
        "nullable": true,
        "oneOf": [
          {
            "$ref": "#/components/schemas/ClasseTeste2"
          }
        ]
      }
    }
  },
  "ClasseTeste2": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "Propriedade1": {
        "type": "string",
        "nullable": true
      },
      "Propriedade2": {
        "type": "string",
        "nullable": true
      },
      "Propriedade3": {
        "type": "integer",
        "format": "int32"
      }
    }
  }

Note: If you use the "swagger" pattern: "2.0", it works well. The problem occurs only with the standard "openapi": "3.0.0".

The inclusion of the following line:
     config.AllowReferencesWithProperties = true;
In my swagger configuration, it solved my problem.

...
services.AddOpenApiDocument(config =>
    {                    config.AddSecurity("apikey", Enumerable.Empty<string>(), new OpenApiSecurityScheme
        {      
            Type = OpenApiSecuritySchemeType.ApiKey,
            Name = "token",
            In = OpenApiSecurityApiKeyLocation.Header,
            Description = "Forneça o token para acessar as funções operacionais da API: Bearer {token}"
        });

        config.OperationProcessors.Add(
            new AspNetCoreOperationSecurityScopeProcessor("bearer"));
        config.Version = "v1";
        config.AllowReferencesWithProperties = true;  /*<<<<<<<<<< HERE*/
        config.Title = "Microsum api.mstiDFE - Documentação";
        config.Description = "" +
            "**API para geração de Documentos Fiscais Eletrônicos (DF-e) do projeto SPED**</br>" +
            "*Subtítulo*</br>" +
            "Token operacional: `ff29894a7a5c7be56a0ac2bfbb4f7b44bbe07ec3`" +
            "" +
            "" +
            "";
    });
...

The inclusion of the following line:
     config.AllowReferencesWithProperties = true;
In my swagger configuration, it solved my problem.

...
services.AddOpenApiDocument(config =>
    {                    config.AddSecurity("apikey", Enumerable.Empty<string>(), new OpenApiSecurityScheme
        {      
            Type = OpenApiSecuritySchemeType.ApiKey,
            Name = "token",
            In = OpenApiSecurityApiKeyLocation.Header,
            Description = "Forneça o token para acessar as funções operacionais da API: Bearer {token}"
        });

        config.OperationProcessors.Add(
            new AspNetCoreOperationSecurityScopeProcessor("bearer"));
        config.Version = "v1";
        config.AllowReferencesWithProperties = true;  /*<<<<<<<<<< HERE*/
        config.Title = "Microsum api.mstiDFE - Documentação";
        config.Description = "" +
            "**API para geração de Documentos Fiscais Eletrônicos (DF-e) do projeto SPED**</br>" +
            "*Subtítulo*</br>" +
            "Token operacional: `ff29894a7a5c7be56a0ac2bfbb4f7b44bbe07ec3`" +
            "" +
            "" +
            "";
    });
...

It worked here. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

saephraim picture saephraim  ·  3Comments

p0wertiger picture p0wertiger  ·  3Comments

PabloInNz picture PabloInNz  ·  4Comments

Rui90 picture Rui90  ·  4Comments

alanedwardes picture alanedwardes  ·  3Comments