Coder Social home page Coder Social logo

ricosuter / njsonschema Goto Github PK

View Code? Open in Web Editor NEW
1.3K 1.3K 520.0 10.7 MB

JSON Schema reader, generator and validator for .NET

Home Page: http://NJsonSchema.org

License: MIT License

C# 96.90% Liquid 2.71% Batchfile 0.01% PowerShell 0.21% Shell 0.17%

njsonschema's Introduction

njsonschema's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

njsonschema's Issues

Filed not letting me have null and expects an object.

Whenever I use something like:

public QueryRule ReportRules { get; set; } or public Validation validation { get; set; }

it won't let it be null, it errors out expecting an object even though I've not set it to required and I need to be able to let the value be null and not have to put an empty object in it's place. have you encountered this before as well?

FlattenInheritanceHierarchy option in generation settings not working

Using version 2.26.5992.40823 from nuget.

JsonSchemaGeneratorSettings settings = new NJsonSchema.Generation.JsonSchemaGeneratorSettings();
settings.DefaultEnumHandling = NJsonSchema.EnumHandling.String;
settings.FlattenInheritanceHierarchy = true;

var generator = new JsonSchemaGenerator(settings);

var schema = JsonSchema4.FromType(typeof(oneofmytypes);
return schema.ToJson();

The generated schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "typeName": "OutgoingRemoteInfoserverDto",
    "additionalProperties": false,
    "properties": {
        "RemoteChannelIdentifier": {
            "type": ["null",
            "string"]
        }
    },
    "allOf": [{
        "type": "object",
        "typeName": "OutgoingHttpDto",
        "additionalProperties": false,
        "required": ["Timeout",
        "EndPoint",
        "RetryOnServerError"],
        "properties": {
            "Timeout": {
                "type": "integer",
                "maximum": 1200.0,
                "minimum": 30.0
            },
            "EndPoint": {
                "type": "string"
            },
            "RetryOnServerError": {
                "type": "boolean"
            }
        },
        "allOf": [{
            "type": "object",
            "typeName": "ChannelBaseDto",
            "additionalProperties": false,
            "required": ["Title"],
            "properties": {
                "ChannelTypeName": {
                    "type": ["null",
                    "string"]
                },
                "ChannelTypeInfo": {
                    "type": ["null",
                    "string"]
                },
                "ID": {
                    "type": ["null",
                    "string"]
                },
                "OwnerBoxId": {
                    "type": "integer"
                },
                "Title": {
                    "type": "string"
                },
                "Comment": {
                    "type": ["null",
                    "string"]
                },
                "Status": {
                    "type": ["null",
                    "string"]
                },
                "Interval": {
                    "type": "integer"
                },
                "IntervalUnit": {
                    "type": ["null",
                    "string"]
                },
                "DateTimeStart": {
                    "type": "string",
                    "format": "date-time"
                },
                "DateTimeStop": {
                    "type": "string",
                    "format": "date-time"
                },
                "DateTimeCreated": {
                    "type": "string",
                    "format": "date-time"
                },
                "DateTimeLastChanged": {
                    "type": "string",
                    "format": "date-time"
                },
                "TemporaryFolderName": {
                    "type": ["null",
                    "string"]
                }
            },
            "allOf": [{
                "type": "object",
                "typeName": "DtoBase",
                "additionalProperties": false
            }]
        }]
    }]
}

shouldnt all properties (including properties for base type) be at the "properties" level?

Extensibility issue

Hey, I would like to know if there is any way to add custom extension data to a property (i.e "sortable : true") to JSchema4 object?

Incorrect handling of concrete IEnumerable types - they have "type:object", not "type:array"

Firstly, thankyou for building such an awesome tool 👍

I've just found a couple of edge-cases that it would be awesome if you could fix :)

Consider the following code:

public class ArrayModel<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator() { return null; }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}
<SNIP>
Console.WriteLine(JsonSchema4.FromType(typeof(ArrayModel<string>)).ToJson());

This will print the correct JSON schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "type": "string"
  }
}

However, there are a couple of ways we can change this that won't generate the correct schema:

BROKEN 1

If we make the type of the IEnumerable concrete

public class ArrayModel : IEnumerable<string>
{
    public IEnumerator<string> GetEnumerator() { return null; }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}
<SNIP>
Console.WriteLine(JsonSchema4.FromType(typeof(ArrayModel)).ToJson());

we find it doesn't print the type of the array items

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {} // Missing type in here.
}

BROKEN 2

Inheriting from a concrete type like List

public class ArrayModel<T> : List<T>
{
}
<SNIP>
Console.WriteLine(JsonSchema4.FromType(typeof(ArrayModel<string>)).ToJson());

produces

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object", // It is treating this as an object, not an array. 
                             // Note that generating from List<string> works correctly.
  "x-typeName": "ArrayModelString",
  "additionalProperties": false,
  "allOf": [
    {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  ]
}

Issue with spinal casing when using NSwag

My urls use spinal casing but when a method is generated in my typescript, ConvertToLowerStartIdentifier and ConvertToUpperStartIdentifier in GeneratorBase only take spaces into consideration. It should replaces dashes with underscores or camel case the value since dashes would break the typescript.

I'm not sure if this belongs here or as an override in ClientGeneratorBase in NSwag.

LoadEnumerations throws an exception when an enum inherits from byte

When an enum which inherits from byte is loaded, the code throws and exeption.

Example:

public enum PermissionFlags : byte
    {
        Create  = 0x01,
        Read    = 0x02,
        Update  = 0x04,
        Delete  = 0x08,
    }

The exception occurs in NJsonSchema (line 248 of JsonScheamGenerator.cs)

var value = (int) Enum.Parse(type, enumName);

"Specified cast is not valid" is the exception

Bug: type array with more than 1 primitive type does not validate correctly

A property schema with a Type that encompasses more than 1 primitive type never validates successfully. Reproducible by adding the following unit test to ObjectValidationTests.cs:

[TestMethod]
public void When_property_matches_one_of_the_types_then_it_should_succeed()
{
    //// Arrange
    var schema = new JsonSchema4();
    schema.Type = JsonObjectType.Object;
    schema.Properties["Foo"] = new JsonProperty
    {
        Type = JsonObjectType.Number | JsonObjectType.Null
    };

    var token = new JObject();
    token["Foo"] = new JValue(5);

    //// Act
    var errors = schema.Validate(token);

    //// Assert
    Assert.AreEqual(0, errors.Count());
}

The errors collection will, in fact, contain 2 NullExpected errors

NSwag: ResponseType WebAPI Annotation - List support C#

I have an endpoint which returns a list of objects in the HttpResponseMessage body. It would be nice to be able to set the ResponseType attribute to a List of objects, like in the snippet below.
[ResponseType(List<object>)]

Sorry, if I missed something when reading the docs, but I am not sure how to get around this issue.

Option to flatten inheritance hierarchy

Add an option to flatten the inheritance hierarchy when generating JSON Schema from a type:

public class AA
{
    public string FirstName { get; set; }
}

public class BB : AA
{
    public string LastName { get; set; }
}

public class CC : BB
{
    public string Address { get; set; }
}

Currently converts to:

{
  "typeName": "CC",
  "additionalProperties": false,
  "type": "object",
  "properties": {
    "Address": {
      "type": "string"
    }
  },
  "allOf": [
    {
      "typeName": "BB",
      "additionalProperties": false,
      "type": "object",
      "properties": {
        "LastName": {
          "type": "string"
        }
      },
      "allOf": [
        {
          "typeName": "AA",
          "additionalProperties": false,
          "type": "object",
          "properties": {
            "FirstName": {
              "type": "string"
            }
          }
        }
      ]
    }
  ],
  "$schema": "http://json-schema.org/draft-04/schema#"
}

And with this new option to:

{
  "typeName": "CC",
  "additionalProperties": false,
  "type": "object",
  "properties": {
    "Address": {
      "type": "string"
    },
    "LastName": {
      "type": "string"
    },
    "FirstName": {
      "type": "string"
    }
  }
}

consider reading schema from a external file

Hi Rico Suter,

I have gone through the provided Demo and Tests of NJsonSchema.Codegeneration . I have found only the following two ways -

  1. From JSon data
  2. From C# Type

But are you supporting reading json schema from external file ? If not will you be able to consider this ?

Thanks,
Vijay.

GetXmlDocumentationPath

Any chance this can scan the bin folder for XML files?

private static string GetXmlDocumentationPath(dynamic assembly)
 {
        var assemblyName = assembly.GetName();
        var path = DynamicPathCombine(DynamicPathGetDirectoryName(assembly.Location), assemblyName.Name + ".xml");
         if (DynamicFileExists(path))
               return path;

         dynamic currentDomain = Type.GetType("System.AppDomain").GetRuntimeProperty("CurrentDomain").GetValue(null);
         path = DynamicPathCombine(currentDomain.BaseDirectory, assemblyName.Name + ".xml");
         if (DynamicFileExists(path))
                    return path;

         // ADD THIS
         path = DynamicPathCombine(currentDomain.BaseDirectory + "bin", assemblyName.Name + ".xml");
         return path;
}

DateParseHandling.DateTimeOffset leads to exceptions during schema validation.

When using DateParseHandling.DateTimeOffset (rather than the lossy default DateParseHandling.DateTime) in Newtonsoft.Json, validating the data against an NJsonSchema.JsonSchema4 results in an InvalidCastException in JsonSchemaValidator.ValidateString.

Attached are two patches: one addition to the UnitTests to demonstrate the problem and one very small one to the JsonSchemaValidator that fixes the problem.

DateTimeOffset-patches.zip

Document supported attributes

public class Model
{
    [JsonProperty("key")]
    [Display(Description = "Some description")]
    public int Key { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }
}
var schema = JsonSchema4.FromType<Model>();
Console.WriteLine(schema.ToJson());
{
  "typeName": "Model",
  "additionalProperties": false,
  "type": "object",
  "required": [
    "key"
  ],
  "properties": {
    "key": {
      "type": "integer"
    },
    "value": {
      "type": "string"
    }
  },
  "$schema": "http://json-schema.org/draft-04/schema#"
}

Support for DefaultValue attribute

Your component is a very good contribution to the community. I've testet it out now and things look great. For now all I miss is support for "default" :) Could you add support for this?

It would be nice to have it through the System.ComponentModel.DefaultValueAttribute

Not able to use JsonSchema4.FromType on UWP

The static initialization of XpathExtensionsType in FullDotNetMethods throws a TypeInitializationException when used in a UWP app. This can be solved using a static constructor and a try/catch, but I wanted to make sure if this is reproducible and I'm not missing anything.

Thanks.

Getting Too Many Properties In Tuple error.

When validating the Json schema against an mvc model, it get the error 'Too Many Properties In Tuple" and the path and property get set to null which errors out my code with "Cannot perform runtime binding on a null reference". Amy ideas on what causes this?

Date format & Pattern is not getting matched.

Hi,
Below data is valid for given schema but we are getting pattern mismatch error.
Sample Schema

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "SimpleDate": {
             "type": "string",
              "format": "date-time"
          },
        "PatternDate": {
            "type": "string",
            "pattern" : "(^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}Z$|^$)"
        }
    }
}

Sample Data

{
  "SimpleDate":"2012-05-18T00:00:00Z",
  "PatternDate":"2012-11-07T00:00:00Z"
}

Here I found two issues.

  1. If SimpleDate format is "date" then, it should accept only "2015-05-18" & not "2012-05-18T00:00:00Z".
  2. Even if the PatternDate value is matching to the PatternDate schema. it is throw PatternMismatch error.

tried to verify it with
http://www.jsonschemavalidator.net/
it does not return any validation error.

Add enumNames property

Add enumNames property, set in schema gen and use in code gen...
Also add StringEnumConverter to string enum properties (C#)...
EnumHandling.Integer should create int enum properties.
(Maybe set default enum handling to integer)

Use default value in code generators

Only applicable for CSharp (TS generates only interfaces):

[DefaultValue(5)]            
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public int Age { get; private set; }

or

public int Age { get; private set; } = 5;

Whats better?

Support for nullable Primitives

The schema generated by NJsonSchema doesn't looks like supporting nullable primitive types. Instead the schema looks similar to that of non-nullable types, for example int, datetime & string. This is a feature request.

Do not generate optional properties in TypeScript

Is it ok if I remove all ? on the generated interface properties? We just had a bug, where the interface was:

interface Person {
     parents: Person[];
}

And someone forgot the "s" postfix:

var x = <Person>{
    parent: { ... }
}

Which didn't yield a compiler error because the parents property is optional...
This is why I want to completely avoid ? (all TS properties are nullable anyways).

What do you think?

Modify format based on custom DataType attribute

Hi,
First of all thanks for a great product.
I'm using this with jdorn/json-editor. This editor uses format elements to describe non-standard format requests, such as, for an object, "format": "grid" displaying that object in a grid rather than tabular format.
Currently you output formatting based on data types, ignoring (I think) the DataTypeAttibute.
It would be good to be able to inject specific formatting requirements through the DataType attribute and specifically the datatype.custom type. Perhaps there's another simpler way to do this, but I would like to be able to influence the format elements in a schema by changing the attributes on the class member declarations.

.NET 4.0 Support

Hi all,

I want to use the library for a project running on .NET 4.0 and we are using VS 2010.
Is there any possibility to use the library under this circumstances?

Not able to use JsonSchema4.FromType with System.Type property in target class

Steps to reproduce:

  1. Use code snippet from Readme.md
  2. Add to Person class property with type System.Type. Please find screenshot below
    http://take.ms/rrUaH

Actual result:
System.InvalidOperationException : Could not find item type of array type 'System.Security.Policy.Evidence'.
at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 110 at NJsonSchema.JsonSchemaGenerator.LoadProperty(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 276 at NJsonSchema.JsonSchemaGenerator.GeneratePropertiesAndInheritance(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 205 at NJsonSchema.JsonSchemaGenerator.GenerateObject(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 199 at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 100
at NJsonSchema.JsonSchemaGenerator.LoadProperty(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 276
at NJsonSchema.JsonSchemaGenerator.GeneratePropertiesAndInheritance(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 205
at NJsonSchema.JsonSchemaGenerator.GenerateObject(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 199
at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 100 at NJsonSchema.JsonSchemaGenerator.LoadProperty(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 276 at NJsonSchema.JsonSchemaGenerator.GeneratePropertiesAndInheritance(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 205 at NJsonSchema.JsonSchemaGenerator.GenerateObject(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 199 at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 100
at NJsonSchema.JsonSchemaGenerator.Generate(Type type, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 53
at NJsonSchema.JsonSchemaGenerator.GenerateInheritance(Type type, JsonSchema4 schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 220
at NJsonSchema.JsonSchemaGenerator.GeneratePropertiesAndInheritance(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 209
at NJsonSchema.JsonSchemaGenerator.GenerateObject(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 199
at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 100 at NJsonSchema.JsonSchemaGenerator.LoadProperty(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 276 at NJsonSchema.JsonSchemaGenerator.GeneratePropertiesAndInheritance(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 205 at NJsonSchema.JsonSchemaGenerator.GenerateObject(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 199 at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 100
at NJsonSchema.JsonSchemaGenerator.LoadProperty(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 276
at NJsonSchema.JsonSchemaGenerator.GeneratePropertiesAndInheritance(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 205
at NJsonSchema.JsonSchemaGenerator.GenerateObject(Type type, TSchemaType schema, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 199
at NJsonSchema.JsonSchemaGenerator.Generate(Type type, IEnumerable`1 attributes, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 100
at NJsonSchema.JsonSchemaGenerator.Generate(Type type, ISchemaResolver schemaResolver) in C:\projects\njsonschema\src\NJsonSchema\JsonSchemaGenerator.cs: line 53
at NJsonSchema.JsonSchema4.FromType(JsonSchemaGeneratorSettings settings) in C:\projects\njsonschema\src\NJsonSchema\JsonSchema4.cs: line 71
at NJsonSchema.JsonSchema4.FromType() in C:\projects\njsonschema\src\NJsonSchema\JsonSchema4.cs: line 53

Properties in base classes are not flattened - Properties are always included even if that DataMember attributes are not in place

http://stackoverflow.com/questions/36731101/how-to-make-sure-that-njsonschema-only-includes-required-fields

There should be only one schema route, to the outside world is does not matter how a domain class looks like.

                var requiredAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RequiredAttribute");
                var jsonPropertyAttribute = property.GetCustomAttribute<JsonPropertyAttribute>();

                var hasJsonNetAttributeRequired = jsonPropertyAttribute != null && (
                    jsonPropertyAttribute.Required == Required.Always ||
                    jsonPropertyAttribute.Required == Required.AllowNull);

                var hasRequiredAttribute = requiredAttribute != null;
                if (hasRequiredAttribute || hasJsonNetAttributeRequired)
                    parentSchema.RequiredProperties.Add(propertyName);

                var isJsonNetAttributeNullable = jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.AllowNull;

                var isNullable = propertyTypeDescription.IsAlwaysRequired == false;
                if (!hasRequiredAttribute && (isNullable || isJsonNetAttributeNullable))
                    jsonProperty.Type = jsonProperty.Type | JsonObjectType.Null;

                dynamic readOnlyAttribute = TryGetAttribute(attributes, "System.ComponentModel.ReadOnlyAttribute");
                if (readOnlyAttribute != null)
                    jsonProperty.IsReadOnly = readOnlyAttribute.IsReadOnly;

                jsonProperty.Description = GetDescription(property, attributes);

                ApplyPropertyAnnotations(jsonProperty, attributes, propertyTypeDescription);`

If a property is not required why is the ApplyPropertyAnnotations still executed?

Date handling: Add support for moment.js and string

Changes:

  • Add generator option/setting
  • ConvertToClassTemplate: Replace "new Date()" with correct code (depending on setting)
  • ConvertToJavaScriptTemplate: Replace "toISOString()" with correct code (depending on setting)

Add extension to describe generics

In JSON Schema/Swagger/OpenAPI you cannot define generics or templates, see: OAI/OpenAPI-Specification#957

This is a proposal how to implement generics with custom extensions.

Define a schema with generic parameters:

{
    "type": "object",
    "title": "KeyValuePair",  
    "properties": {
        "key": {
            "x-generic": "TKey"
        }, 
        "value": {
            "x-generic": "TValue"
        }
    }
}

Usage of the generic schema in a property:

{
    "properties": {
        "myPairProperty": {
            "x-generics": {
                "TKey": {
                    "type": "string"
                },
                "TValue": {
                    "type": "number"
                }
            }, 
            "oneOf": [
                {
                     "$ref": "#/definitions/KeyValuePair"
                }
            ]
        }
    }
}
  • x-generic defined on JsonProperty: Defines a generic property (if one property has x-generic set, then the property and schema is generic). If the processor understands x-generic, it will ignore all other properties and instead replace the schema with the specified one. Otherwise you can define a default schema for processors which do not support this.
  • x-generics defined on JsonSchema4: Defines the type of the generic parameters (i.e. an instance of the generic schema). If the property schema is generic, then all generic parameters from the generic schema must be defined in x-generics.
    • Update ActualPropertySchema (just for generator): Create copy of generic schema, replace the type parameters, and return it...
  • Update JsonSchemaValidator

It’s important to note that this will only work with the complete NJsonSchema/NSwag toolchain and will probably break or yield wrong results when used with other tools. Ideally we’d also generate a reasonable fallback with eg any typing etc...

Support allOf with code generator

Sample JSON (single inheritance, properties defined in allOf):

"Pet": {
  "type": "object",
  "allOf": [
    {
      "$ref": "#/definitions/NewPet"
    },
    {
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64"
        }
      }
    }
  ]
},
"NewPet": {
  "type": "object",
  "required": [
    "name"
  ],
  "properties": {
    "name": {
      "type": "string"
    },
    "tag": {
      "type": "string"
    }
  }
},

Should generate in TypeScript:

export interface Pet implements NewPet {
         id: integer; 
}

export interface NewPet {
    name: string;
    tag?: string;
}

Support for handling different Property Name conventions

When using Json.NET CamelCasePropertyNamesContractResolver the serialized data has camel case property names. However NJsonSchema generates upper-case property names (based on reflection).

It would be beneficial to allow users to control the naming convention of the generated property names.

Suggested solution:

  1. Define enum PropertyNameHandling { Default, CamelCase }
  2. Add a new option DefaultPropertyNameHandling to JsonSchemaGeneratorSettings
  3. Pass the naming convention to Property.GetName()

Need schema validation for an object

In my c# I have a public object value { get; set; } and in my json I have a key value pair where the key will always be a string but the value might be an integer, boolean or string so i cannot implicity set this as one of those three in the model. I need it to be flexible. For some reason the schema errors out with object expected. How can i get around this?

public List<string> operators { get; set; } is causing validation errors

I'm generating a json schema from a class and everything works except: public Dictionary<string, object> values { get; set; }.

For some reason it converts it to:

"values": { "type": [ "null", "object" ], "additionalProperties": { "type": "object" } },.

What the json should look like is this:

"values": { "employee Only": "Employee Only", "employee and Spouse": "Employee and Spouse", "employee and Children": "Employee and Children", "employee and Family": "Employee and Family" }

This is causing an error to be thrown when it shouldn't.

If you want me to post the fully convert Json Schema, I can but it is quite long. Hopefully this makes sense. I'm thinking the "additionalProperties" is what is causing the error. Am I doing something wrong maybe?

Schema4.Items not working as expected

NJsonSchemaItemsTest.zip
Attached is a recursive test that compares how NewtonSoft can get hold of all Properties and Items for a given Schema, while NJsonSchema does not get the Items' properties...

Schema:

{
  "typeName": "ClassRoom",
  "additionalProperties": false,
  "type": "object",
  "required": [
    "Id"
  ],
  "properties": {
    "Id": {
      "type": "integer"
    },
    "Name": {
      "type": "string"
    },
    "Size": {
      "type": [ "integer", "null" ]
    },
    "Students": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "Age": {
            "type": [ "integer", "null" ]
          },
          "Name": {
            "type": [ "string", "null" ]
          }
        }
      }
    }
  },
  "$schema": "http://json-schema.org/draft-04/schema#"
}

Output of the test:

NJsonSchema - Properties >> Items:
NJsonSchema Property: Id
NJsonSchema Property: Name
NJsonSchema Property: Size
NJsonSchema Property: Students

Newtonsoft.Json.Schema - Properties >> Items:
Newtonsoft Property: Id
Newtonsoft Property: Name
Newtonsoft Property: Size
Newtonsoft Property: Students
*Newtonsoft Item:  |
Newtonsoft Property: Age
Newtonsoft Property: Name*

Support customizing the generated property and type names

It would be nice if there was a way to (optionally) customize the names for generated properties and types. For instance, we could add few optional generator settings that would allow passing in lambdas for customizing the naming. Lambdas would receive the type|property name from json schema as input, and return the name to use in the generated code. Something like this:

            Func<string, string> namingTransform = (name) => {
                // some_name-b64  ==> SomeName_b64
                var components = name.Split('_');
                var transformedComponents =  components
                        .Select((c) => c.First().ToString().ToUpperInvariant() + c.Substring(1))
                        .Select((c) => c.Replace("-", "_"));
                return string.Join("", transformedComponents);
            };
            var settings = new CSharpGeneratorSettings() {
                    ClassStyle = CSharpClassStyle.Poco,
                    PropertyNameFun = namingTransform, 
                    TypeNameTransformFun = namingTransform 
            };

So given schema

        {
                '$schema': 'http://json-schema.org/draft-04/schema#',
                'id': 'http://domain.name.com/foo.json#',
                'type': 'object',
                'additionalProperties': false,
                'properties': {
                    'foo_bar-b64': {
                        'type': 'string'
                     }
                }
         }

Generated code would end up looking something like this

    public class Foo {
        public string FooBar_b64 {get;set;}
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.