Nswag: basePath not being deserialized when when using C# to convert swagger.json to SwaggerDocument

Created on 22 Apr 2018  路  2Comments  路  Source: RicoSuter/NSwag

Hey,
When I convert a swagger.json document to SwaggerDocument object in C#, it ignores the basePath variable and sets it to null. I am using swagger 2.0, in which it should not ignore that property. I suspect it has to do with the following annotation:

[Newtonsoft.Json.JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = 6, PropertyName = "basePath")]

My example swagger.json file:

{
    "swagger": "2.0",
    "info": {
        "description": "",
        "version": "2.15.250",
        "title": "Test Service",
        "contact": {
            "name": "HelpDesk",
            "email": "[email protected]"
        },
        "license": {
            "name": "All rights reserved",
            "url": ""
        }
    },
    "basePath": "/api/example/v1"

}

Here's what I use to deserialize json to SwaggerDocument object:

class Program
    {
        static void Main(string[] args)
        {
            string json = System.IO.File.ReadAllText(@"c:/desktop/swagger.json");
            SwaggerDocument doc = JsonConvert.DeserializeObject<SwaggerDocument>(json);
            Console.WriteLine(doc.BasePath); 
            var name = Console.ReadLine();
            Console.WriteLine(doc.ToJson());
            Console.ReadLine();

        }
    }

and the output is:

{
    "swagger": "2.0",
    "info": {
        "title": "Test Service",
        "description": "",
        "contact": {
            "name": "HelpDesk",
            "email": "[email protected]"
        },
        "license": {
            "name": "All rights reserved",
            "url": ""
        },
        "version": "2.15.250"

    }
}

image

question

All 2 comments

You cant serialize/deserialize a document with JsonConvert. Use ToJson() (or FromJsonAsync)

Thanks Rico! that worked.

SwaggerDocument.FromJsonAsync(json)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rui90 picture Rui90  路  4Comments

Peter554 picture Peter554  路  3Comments

blackbaud-GavinNicol picture blackbaud-GavinNicol  路  3Comments

Rui90 picture Rui90  路  3Comments

molszews picture molszews  路  4Comments