Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template in ASP.NET Core results in wrong swagger #1435

Closed
bakashinji opened this issue Jul 4, 2018 · 6 comments
Closed

Template in ASP.NET Core results in wrong swagger #1435

bakashinji opened this issue Jul 4, 2018 · 6 comments

Comments

@bakashinji
Copy link

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

@RicoSuter
Copy link
Owner

Is this the same issue? RicoSuter/NJsonSchema#732

@bakashinji
Copy link
Author

bakashinji commented Jul 4, 2018

I think they are the same issue, but with regards to generics rather than inheritance. Also the fix @fizmike suggested in his second post (moving all the "properties" into the "allOf" array) is exactly what gets it to work with swagger codegen and the swagger-parser

I didn't realize this was a problem with swagger2 standard compliancy

@RicoSuter
Copy link
Owner

RicoSuter commented Jul 4, 2018

moving all the "properties" into the "allOf" array

I've never seen a statement that having the properties in the root and allOf is not allowed... but it seems that some generators cannot handle that...

@bakashinji
Copy link
Author

Actually the parser in https://github.com/swagger-api/swagger-parser creates a model which doesn't have the SingleResponse in the PagedResponseOfSingleResponse, which is why I asked there first

@RicoSuter
Copy link
Owner

Created a PR with current attempt to this: RicoSuter/NJsonSchema#733

@RicoSuter
Copy link
Owner

Fixed in v11.20.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants