docfx icon indicating copy to clipboard operation
docfx copied to clipboard

Support REST API

Open chenkennt opened this issue 10 years ago • 30 comments

chenkennt avatar Feb 29 '16 09:02 chenkennt

It's now supported. docfx-seed adds 2 swagger files into the project https://github.com/docascode/docfx-seed/tree/master/restapi and the result looks like: http://docascode.github.io/docfx-seed/restapi/petstore.html

vicancy avatar May 11 '16 02:05 vicancy

Hello @vicancy , I have created a swagger file by using NSwag tool. I have placed the json file inside a "restapi" folder and then added a toc.md as you have made in your example.

I am getting an exception when building the docs which says

Error: System.AggregateException: One or more errors occurred. ---> 
Newtonsoft.Json.JsonException: type is already defined in referenced object "#/definitions/XXXXXX".
   at Microsoft.DocAsCode.Build.RestApi.Swagger.Internals.SwaggerObjectConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) in C:\Users\qinzhu\workspace\docfx-latest\src\Microsoft.DocAsCode.Build.RestApi\Swagger\Internals\SwaggerObjectConverter.cs:line 40

Any suggestion?

vdecristofaro avatar May 13 '16 22:05 vdecristofaro

@vdecristofaro Is the swagger file contains such code block:

{
    "$ref": "#/definitions/XXXX",
    "type": "XX"
}

and inside #/definitions/XXXX it also contains key type? Such case it throws exception.

vicancy avatar May 14 '16 23:05 vicancy

@vicancy Right.

this is an extract of the generated swagger

    "cp/redeem": {
      "Put": {
        "tags": [],
        "operationId": "Coupon_RedeemCoupon",
        "parameters": [
          {
            "name": "model",
            "in": "body",
            "required": false,
            "schema": {
              "type": [
                "null",
                "object"
              ],
              "$ref": "#/definitions/CouponRedeemViewModel"
            }
          }
        ],
        "responses": {
          "200": {
            "schema": {
              "type": "file"
            }
          }
        }
      }
    },

How would you then advice to solve this problem. Is this an issue with docfx?

vdecristofaro avatar May 15 '16 11:05 vdecristofaro

why there is extra


              "type": [
                "null",
                "object"
              ],

inside schema? it is not valid. I would suggest remove that block.

vicancy avatar May 15 '16 12:05 vicancy

Hello @vicancy as you can see in my referenced issue that has been solved and now generate exactly the code reported by the NSwag project owner. I have also updated the docfx to 1.9.3. However I am still getting the same error .

Info: [Build Document]5 plug-in(s) loaded.
Info: [Build Document]Max parallelism is 8.
Info: [Build Document]Markdown engine is dfm
Info: Completed building documents in 1078,1519 milliseconds.
Info: Completed executing in 1239,7334 milliseconds.
Error: System.AggregateException: One or more errors occurred. ---> Newtonsoft.Json.JsonException: type is already defined in referenced object "#/definitions/XXXXX".

the generated code now is as follow

    "cp/redeem": {
      "Put": {
        "tags": [],
        "operationId": "Coupon_RedeemCoupon",
        "parameters": [
          {
            "name": "model",
            "in": "body",
            "required": false,
            "schema": {
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "$ref": "#/definitions/CouponOperationViewModel"
                }
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "schema": {
              "type": "file"
            }
          }
        }
      }
    },

Could you please tell me how did you generate your json sample?

Thank you!

vdecristofaro avatar May 18 '16 23:05 vdecristofaro

@vdecristofaro could you provide the json file or github repo for us? I will investigate for you. Thanks.

hellosnow avatar May 27 '16 05:05 hellosnow

Sure @hellosnow. Here is a simple file I am using as a sample. the file is generated by using NSwag tool which by using the WebApiToSwagger generator described here.

Thanks!

tmp_swagger.txt

vdecristofaro avatar May 27 '16 12:05 vdecristofaro

@vdecristofaro , I checked your attached swagger json and found its schema is invalid. Could you work with NSwag tool to generate the valid swagger json file? I use http://bigstickcarpet.com/swagger-parser/www/index.html to validate the swagger file, for example "typeName", "enumNames" and "oneOf" is not valid.

Some valid swagger files example: http://petstore.swagger.io/v2/swagger.json Swagger schema: http://swagger.io/specification/

hellosnow avatar May 30 '16 07:05 hellosnow

We also have plan to validate the swagger schema #312

hellosnow avatar May 30 '16 07:05 hellosnow

@hellosnow First of all thank you for your answer. This is getting very strange and I can't understand the way to solve this. I have tried to pass a Swashbuckle generated json to the bigstickcarpet parser and it tells me that is invalid. More specifically it says: This API is valid, but it cannot be shown because it contains circular references Also NSwag generated file is invalid as you have already seen. I am using NSwag however only for the purpose of generating different swagger for each controller.

Could you please advice which kind of swagger generator should I use to create my swagger file?

vdecristofaro avatar May 30 '16 22:05 vdecristofaro

Hi @vdecristofaro ,

  • For "circular references", as the validation tool hints, that's an invalid case and docfx doesn't support it. For example, if A's type is defined as B while B's type is defined as A, docfx could not resolve the real type.
  • For NSwag generated invalid json, I suggest you to file some issue and see whether they can help you to address the root cause to lead to invalid swagger file? Is it code bug of NSwag or incorrect type reference definition in your client/server side?

Considering not sure which language you're using to write for the client/server side, here's the swagger official tools: http://swagger.io/tools/, could you check if there's any suitable for you?

hellosnow avatar Jun 01 '16 09:06 hellosnow

Hi @hellosnow, My project is an ASP.NET web api 2.2 project (.Net 4.6.1). Let me give you a bit of reference on what I am doing

  • I have integrated the Swashbuckle nuget package in the web api and I am really enjoying the swagger documentation generated. To create a much more detailed documentation for my API I am evaluating DocFX. Because DocFX require a single json for every API I have also filled an issue to Swashbuckle to ask if it is possible to generate a single json for every controller.
  • Regarding NSwag. I have done exactly what you suggest here. The project owner told me that the problem seems to be related to the fact that Newtonsoft does not handle JSON Schema v4 as specified by the Swagger specification. You can follow the entire discussion here. As the NSwag project owner says in the issue I dont really understand why a path in the file get validated with the message Additional properties not allowed: cp/redeem,cp/create at #/paths.

Apart from this, I still do not understand where the circular reference would be. The Swashbuckle generated JSON is correctly handled by the swagger-ui package which shows the generated documentation. I really dont know how to proceed here.

vdecristofaro avatar Jun 01 '16 22:06 vdecristofaro

@vdecristofaro I will look into the Swashbuckle and NSwag tomorrow carefully and give you update later. But I just did one testing on official swagger io and found it cannot be processed:

  1. Open http://petstore.swagger.io/
  2. Enter your swagger url: https://raw.githubusercontent.com/hellosnow/docfx-seed/issue181/restapi/tmp_swagger.json, which I just uploaded in my repo.
  3. Click Explore.
  4. Check error report at http://online.swagger.io/validator/debug?url=https://raw.githubusercontent.com/hellosnow/docfx-seed/issue181/restapi/tmp_swagger.json
  5. the validation error is same with the one in http://bigstickcarpet.com/swagger-parser/www/index.html?
  • Additional properties not allowed: cp/redeem,cp/create at #/paths.
  • TypeName and EnumName

hellosnow avatar Jun 02 '16 10:06 hellosnow

In NSwag v2.43, the missing / prefix is added to the paths and the typeName and enumNames fields are prefixed with x- (custom fields). Now the generated Swagger spec should be valid...

RicoSuter avatar Jun 02 '16 22:06 RicoSuter

Thanks @rsuter for the quick update. @vdecristofaro could you try to generate the new Swagger json file by NSwag v2.43? We can try to parse the new generated json file in docfx and see whether it works.

hellosnow avatar Jun 03 '16 03:06 hellosnow

I did it. DocFX now still end with error during the build

Info: Completed building documents in 2208,5322 milliseconds.
Info: Completed executing in 3194,5989 milliseconds.
Error: System.AggregateException: One or more errors occurred. ---> Newtonsoft.Json.JsonException: type is already defined in referenced object "#/definitions/ReceiptProduct". at Microsoft.DocAsCode.Build.RestApi.Swagger.Internals.SwaggerObjectConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer)

I have tried to parse the resulting json at http://bigstickcarpet.com/swagger-parser/www/index.html and i get the following error

Swagger schema validation failed. 
  Additional properties not allowed: oneOf at #/definitions/Payment/properties/Method

JSON_OBJECT_VALIDATION_FAILED
```.

I am attaching the new json file here. Thanks for helping




[tmp_swagger.txt](https://github.com/dotnet/docfx/files/297244/tmp_swagger.txt)


vdecristofaro avatar Jun 03 '16 08:06 vdecristofaro

Ok, now the problem is probably that JSON.net just supports JSON Schema v3, but Swagger requires v4. See link at bottom: https://github.com/json-schema/json-schema/wiki/anyOf,-allOf,-oneOf,-not As I told, I implemented the whole Swagger object model + JSON Schema v4 in http://nswag.org (nuget: NSwag.Core)

RicoSuter avatar Jun 03 '16 08:06 RicoSuter

Seems oneOf is not supported by swagger? Discussions are on Support oneOf: "Swagger use an extended subset of JSON Schema, and explicitly don't support oneOf", refer to Swagger schema definition

hellosnow avatar Jun 03 '16 14:06 hellosnow

Oh well, what a mess this Swagger is...

RicoSuter avatar Jun 03 '16 15:06 RicoSuter