question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template in ASP.NET Core results in wrong swagger

See original GitHub issue

This code, in different files

public class PagedResponse<T> : PagedResultBase
        where T : class
{

	public ICollection<T> Results { get; set; }


	public PagedResponse()
	{
		Results = new List<T>();
	}
}

public sealed class SingleResponse
{
	Guid SomeInfo;
}
	
[HttpPost]
public Task<PagedResponse<SingleResponse>> Api([FromBody] PagedRequest request)
{
	// return the Item ...
}

results in the following swagger

{
  "x-generator": "NSwag v11.16.1.0 (NJsonSchema v9.10.41.0 (Newtonsoft.Json v9.0.0.0))",
  "swagger": "2.0",
  "info": {
    "title": "My Title",
    "version": "1.0.0"
  },
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/api/v0/my/api": {
      "post": {
        "tags": [
          "My"
        ],
        "operationId": "my_api",
        "parameters": [
          {
            "name": "request",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/PagedRequest"
            },
            "x-nullable": true
          }
        ],
        "responses": {
          "200": {
            "x-nullable": true,
            "description": "",
            "schema": {
              "$ref": "#/definitions/PagedResponseOfSingleResponse"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "PagedRequest": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "page",
        "pageSize"
      ],
      "properties": {
        "id": {
          "type": "string",
          "format": "guid"
        },
        "page": {
          "type": "integer",
          "format": "int32"
        },
        "pageSize": {
          "type": "integer",
          "format": "int32"
        }
      }
    },
    "PagedResponseOfSingleResponse": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "results": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/SingleResponse"
          }
        }
      },
      "allOf": [
        {
          "$ref": "#/definitions/PagedResultBase"
        }
      ]
    },
    "SingleResponse": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "someInfo"
      ],
      "properties": {
        "someInfo": {
          "type": "string",
          "format": "guid"
        }
      }
    },
    "PagedResultBase": {
      "type": "object",
      "x-abstract": true,
      "additionalProperties": false,
      "required": [
        "currentPage",
        "pageSize"
      ],
      "properties": {
        "currentPage": {
          "type": "integer",
          "format": "int32"
        },
        "pageSize": {
          "type": "integer",
          "format": "int32"
        }
      }
    }
  }
}

The part of PagedResponseOfSingleResponse is wrong and results in an empty Model if parsed by the swagger parse (apart from objects from PagedResultBase) and should be:

"PagedResponseOfSingleResponse": {
	"type": "object",
	"allOf": [
		{
			"$ref": "#/definitions/PagedResultBase"
		},
		{
			"additionalProperties": false,
			"properties": {
				"results": {
					"type": "array",
					"items": {
						"$ref": "#/definitions/SingleResponse"
					}
				}
			}
		}
	]
}

which results in a correct model

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RicoSutercommented, Sep 28, 2018

Fixed in v11.20.0

0reactions
RicoSutercommented, Jul 4, 2018

Created a PR with current attempt to this: https://github.com/RSuter/NJsonSchema/pull/733

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET CORE Web API with Swagger produces wrong ...
This code snippets are showing a Brand from my Model. How can I fix it so that Swagger takes the property names as...
Read more >
Handle errors in ASP.NET Core web APIs
Show 2 more. This article describes how to handle errors and customize error handling with ASP.NET Core web APIs.
Read more >
Mastering Web APIs with Swagger, ApiExplorer and NSwag
Mastering External Web API's in ASP.Net Core and ABP with Swagger, ApiExplorer, and NSwag. How to expose a second Web API in Swagger...
Read more >
Angular WebAPI Project using .NET Core gets '404 Not ...
NET Core gets '404 Not Found' or runs on wrong port. I've got an Angular application that runs using .NET Core in a...
Read more >
Handling errors in ASP.NET Core Web API - DevTrends
Handling errors in an ASP.NET Core Web API. This post looks at the best ways to handle exceptions, validation and other invalid requests...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found