ricosuter / njsonschema Goto Github PK
View Code? Open in Web Editor NEWJSON Schema reader, generator and validator for .NET
Home Page: http://NJsonSchema.org
License: MIT License
JSON Schema reader, generator and validator for .NET
Home Page: http://NJsonSchema.org
License: MIT License
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;
}
..
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?
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?
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.
tried to verify it with
http://www.jsonschemavalidator.net/
it does not return any validation 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?
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?
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*
Steps to reproduce:
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, IEnumerable
1 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.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, IEnumerable
1 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
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;
}
Generate classes instead of interfaces and provide methods to copy data into new instances.
Support ISO date time format by converting string values to Date
(only possible with TS classes): #43
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.
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:
DefaultPropertyNameHandling
to JsonSchemaGeneratorSettings
Property.GetName()
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?
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.
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.
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?
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.
https://raw.githubusercontent.com/fge/sample-json-schemas/master/geojson/geometry.json
Branch: one-of-inheritance
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:
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.
}
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"
}
}
]
}
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?
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?
See title
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
.
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...
It would be nice if top level type name could be derived from the schema id - this would allow code generation from schemas without adding typeName fields.
For instance, if schema id is "http://domain.name.com/foo.json#" then type name for the top level type would become 'foo' (instead of Anonymous).
...
Handle byte and byte[] when generating schema
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"
}
}
}
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.
Hi Rico Suter,
I have gone through the provided Demo and Tests of NJsonSchema.Codegeneration . I have found only the following two ways -
But are you supporting reading json schema from external file ? If not will you be able to consider this ?
Thanks,
Vijay.
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?
Generate xml doc from schema
Such as Required
, PropertyName
, NullValueHandling
. I don't know how to use other properties.
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;}
}
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
Changes:
Default ObservableCollection, or IList, List, etc
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)
C#:
Generic<T>
Typescript
Generic[T] // wrong syntax
it will be better if sharing generic open type
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.
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#"
}
CSharp: Initialize property with default if possible
TypeScript: Not possible because interface are generated (solution?)
See #25
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?
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
..
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.