3

Swagger Json is malformed in the sense that it cannot be serialized or deserialized in many languages because it allows for properties with illegal variable names such as forward slash. Take this common example from the Swagger website:

http://petstore.swagger.io/v2/swagger.json

If you copy and past the json in to this common Json -> C# converter, you get an error. Try it out:

http://json2csharp.com/

It kind of works here: https://www.freecodeformat.com/json2csharp.php

But, this is the C# that is generated:

public class Paths
{
    /// <summary>
    /// 
    /// </summary>
    public /pet /pet { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /pet/findByStatus /pet/findByStatus { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /pet/findByTags /pet/findByTags { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /pet/{petId} /pet/{petId} { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /pet/{petId}/uploadImage /pet/{petId}/uploadImage { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /store/inventory /store/inventory { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /store/order /store/order { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /store/order/{orderId} /store/order/{orderId} { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /user /user { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /user/createWithArray /user/createWithArray { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /user/createWithList /user/createWithList { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /user/login /user/login { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /user/logout /user/logout { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public /user/{username} /user/{username} { get; set; }
}

Note: it is totally malformed.

Is there a way to get this stuff in and out of an object model without resorting to trawling through the object model with Newtonsoft's JObjects etc.?

Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
  • [JsonPropertyAttribute Class](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm) You could also use a custom converter – Ňɏssa Pøngjǣrdenlarp Jan 17 '18 at 02:35
  • My question was: Is there a way to get this stuff in and out of an object model without resorting to trawling through the object model with Newtonsoft's JObjects etc.? – Christian Findlay Jan 17 '18 at 02:37
  • Yeah. Decorate the class with the attribute to map `/pet` data to `Pet` or whatever you want. Its a deserializing aide not a parsing thing. Example: https://stackoverflow.com/q/45095063/1070452 With your own converter you could do it yourself. – Ňɏssa Pøngjǣrdenlarp Jan 17 '18 at 02:41
  • Excellent. Thanks. Do you want to write that up with a bit more detail so I can mark it as the answer? – Christian Findlay Jan 17 '18 at 02:52
  • You could just dupe it to that link - I am not *certain* is will work with that JSON. It works to mask out other keywords like return or Error (vb). – Ňɏssa Pøngjǣrdenlarp Jan 17 '18 at 02:55
  • That link doesn't answer my question specifically. You should document it and grab the rep. – Christian Findlay Jan 17 '18 at 03:35
  • Perhaps `paths` should be deserialized to a `Dictionary` for some appropriate `PathData`? See [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182). – dbc Jan 17 '18 at 08:39
  • swashbuckle does exactly that! have you looked into it? – Helder Sepulveda Jan 17 '18 at 15:33
  • 1
    Here is what you need: https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Swagger/SwaggerDocument.cs – Helder Sepulveda Jan 17 '18 at 15:44

0 Answers0