Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support standalone create fixes #1586 #1585

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/NJsonSchema/Generation/SampleJsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ public class SampleJsonSchemaGenerator
{
/// <summary>Generates the JSON Schema for the given JSON data.</summary>
/// <param name="json">The JSON data.</param>
/// <param name="standalone">Create standalone schema without references</param>
/// <returns>The JSON Schema.</returns>
public JsonSchema Generate(string json)
public JsonSchema Generate(string json, bool standalone = false)
{
var token = JsonConvert.DeserializeObject<JToken>(json, new JsonSerializerSettings
{
DateFormatHandling = DateFormatHandling.IsoDateFormat
});

var schema = new JsonSchema();
Generate(token, schema, schema, "Anonymous");
Generate(token, schema, schema, "Anonymous", standalone);
return schema;
}

Expand All @@ -50,11 +51,11 @@ public JsonSchema Generate(Stream stream)
var token = serializer.Deserialize<JToken>(jsonReader);

var schema = new JsonSchema();
Generate(token, schema, schema, "Anonymous");
Generate(token, schema, schema, "Anonymous", false);
return schema;
}

private void Generate(JToken token, JsonSchema schema, JsonSchema rootSchema, string typeNameHint)
private void Generate(JToken token, JsonSchema schema, JsonSchema rootSchema, string typeNameHint, bool standalone)
{
if (schema != rootSchema && token.Type == JTokenType.Object)
{
Expand All @@ -70,21 +71,21 @@ private void Generate(JToken token, JsonSchema schema, JsonSchema rootSchema, st
properties.All(p => s.Properties.ContainsKey(p.Name)));
}

if (referencedSchema == null)
if (referencedSchema == null || standalone)
{
referencedSchema = new JsonSchema();
AddSchemaDefinition(rootSchema, referencedSchema, typeNameHint);
}

schema.Reference = referencedSchema;
GenerateWithoutReference(token, referencedSchema, rootSchema, typeNameHint);
GenerateWithoutReference(token, referencedSchema, rootSchema, typeNameHint, standalone);
return;
}

GenerateWithoutReference(token, schema, rootSchema, typeNameHint);
}

private void GenerateWithoutReference(JToken token, JsonSchema schema, JsonSchema rootSchema, string typeNameHint)
private void GenerateWithoutReference(JToken token, JsonSchema schema, JsonSchema rootSchema, string typeNameHint, bool standalone)
{
if (token == null)
{
Expand All @@ -94,11 +95,11 @@ private void GenerateWithoutReference(JToken token, JsonSchema schema, JsonSchem
switch (token.Type)
{
case JTokenType.Object:
GenerateObject(token, schema, rootSchema);
GenerateObject(token, schema, rootSchema, standalone);
break;

case JTokenType.Array:
GenerateArray(token, schema, rootSchema, typeNameHint);
GenerateArray(token, schema, rootSchema, typeNameHint, standalone);
break;

case JTokenType.Date:
Expand Down Expand Up @@ -161,7 +162,7 @@ private void GenerateWithoutReference(JToken token, JsonSchema schema, JsonSchem
}
}

private void GenerateObject(JToken token, JsonSchema schema, JsonSchema rootSchema)
private void GenerateObject(JToken token, JsonSchema schema, JsonSchema rootSchema, bool standalone)
{
schema.Type = JsonObjectType.Object;
foreach (var property in ((JObject)token).Properties())
Expand All @@ -170,19 +171,19 @@ private void GenerateObject(JToken token, JsonSchema schema, JsonSchema rootSche
var propertyName = property.Value.Type == JTokenType.Array ? ConversionUtilities.Singularize(property.Name) : property.Name;
var typeNameHint = ConversionUtilities.ConvertToUpperCamelCase(propertyName, true);

Generate(property.Value, propertySchema, rootSchema, typeNameHint);
Generate(property.Value, propertySchema, rootSchema, typeNameHint, standalone);
schema.Properties[property.Name] = propertySchema;
}
}

private void GenerateArray(JToken token, JsonSchema schema, JsonSchema rootSchema, string typeNameHint)
private void GenerateArray(JToken token, JsonSchema schema, JsonSchema rootSchema, string typeNameHint, bool standalone)
{
schema.Type = JsonObjectType.Array;

var itemSchemas = ((JArray)token).Select(item =>
{
var itemSchema = new JsonSchema();
GenerateWithoutReference(item, itemSchema, rootSchema, typeNameHint);
GenerateWithoutReference(item, itemSchema, rootSchema, typeNameHint, standalone);
return itemSchema;
}).ToList();

Expand Down