Skip to content

Commit

Permalink
🎉 handle nullable defaults in generating default (example) jsons
Browse files Browse the repository at this point in the history
  • Loading branch information
philipshield committed Feb 5, 2024
1 parent 3585d60 commit 57e4917
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
22 changes: 11 additions & 11 deletions src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void When_sample_data_is_generated_from_schema_then_properties_are_set()
{
//// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<Person>();
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();

//// Act
var token = generator.Generate(schema);
Expand All @@ -60,7 +60,7 @@ public void When_sample_data_is_generated_from_schema_with_base_then_properties_
{
//// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<Student>();
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();

//// Act
var token = generator.Generate(schema);
Expand All @@ -79,7 +79,7 @@ public void Default_values_are_set_for_arrays()
{
//// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<Measurements>();
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();

//// Act
var token = generator.Generate(schema);
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task When_generateOptionalProperties_is_false_then_optional_propert
}";

var schema = await JsonSchema.FromJsonAsync(data);
var generator = new SampleJsonDataGenerator(new SampleJsonDataGeneratorSettings
var generator = new DefaultJsonDataGenerator(new DefaultJsonDataGeneratorSettings
{
GenerateOptionalProperties = false
});
Expand Down Expand Up @@ -174,7 +174,7 @@ public async Task PropertyWithIntegerMinimumDefiniton()
}
}
}";
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);
Expand Down Expand Up @@ -231,7 +231,7 @@ public async Task SchemaWithRecursiveDefinition()
}
}
}";
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);
Expand Down Expand Up @@ -275,7 +275,7 @@ public async Task GeneratorAdheresToMaxRecursionLevel()
}
}
}";
var generator = new SampleJsonDataGenerator(new SampleJsonDataGeneratorSettings() { MaxRecursionLevel = 2 });
var generator = new DefaultJsonDataGenerator(new DefaultJsonDataGeneratorSettings() { MaxRecursionLevel = 2 });
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);
Expand Down Expand Up @@ -335,7 +335,7 @@ public async Task SchemaWithDefinitionUseMultipleTimes()
}
}
}";
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);

//// Act
Expand Down Expand Up @@ -390,7 +390,7 @@ public async Task PropertyWithFloatMinimumDefinition()
}
}
}";
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);
Expand Down Expand Up @@ -440,7 +440,7 @@ public async Task PropertyWithDefaultDefiniton()
}
}
}";
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);
Expand Down Expand Up @@ -494,7 +494,7 @@ public async Task PropertyExclusiveMinimumDefiniton()
}
}
}";
var generator = new SampleJsonDataGenerator();
var generator = new DefaultJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace NJsonSchema
{
/// <summary> Settings for generating sample json data.</summary>
public class SampleJsonDataGeneratorSettings
public class DefaultJsonDataGeneratorSettings
{
/// <summary>Gets or sets a value indicating whether to generate optional properties (default: true).</summary>
public bool GenerateOptionalProperties { get; set; } = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,17 @@
namespace NJsonSchema.Generation
{
/// <summary>Generates a sample JSON object from a JSON Schema.</summary>
public class SampleJsonDataGenerator
public class DefaultJsonDataGenerator
{
private readonly SampleJsonDataGeneratorSettings _settings;
private readonly DefaultJsonDataGeneratorSettings _settings;

/// <summary>
/// Initializes a new instance of <see cref="SampleJsonDataGenerator"/> class with default settings..
/// </summary>
public SampleJsonDataGenerator()
{
_settings = new SampleJsonDataGeneratorSettings();
}

/// <summary>
/// Initializes a new instance of <see cref="SampleJsonDataGenerator"/> class.
/// Initializes a new instance of <see cref="DefaultJsonDataGenerator"/> class.
/// </summary>
/// <param name="settings">The settings to use.</param>
public SampleJsonDataGenerator(SampleJsonDataGeneratorSettings settings)
public DefaultJsonDataGenerator(DefaultJsonDataGeneratorSettings? settings = null)
{
_settings = settings;
_settings = settings ?? new DefaultJsonDataGeneratorSettings();
}

/// <summary>Generates a sample JSON object from a JSON Schema.</summary>
Expand Down Expand Up @@ -71,6 +63,10 @@ private JToken Generate(JsonSchema schema, Stack<JsonSchema> schemaStack)

return obj;
}
else if (schema.Default == null && schema.IsNullable(SchemaType.JsonSchema))
{
return JValue.CreateNull();
}
else if (schema.Default != null)
{
return JToken.FromObject(schema.Default);
Expand Down
10 changes: 9 additions & 1 deletion src/NJsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,17 @@ public string ToJson(Formatting formatting)

/// <summary>Generates a sample JSON object from a JSON Schema.</summary>
/// <returns>The JSON token.</returns>
[Obsolete("Use ToDefaultJson() instead.")]
public JToken ToSampleJson()
{
var generator = new SampleJsonDataGenerator();
return ToDefaultJson();
}

/// <summary>Generates a default JSON object from a JSON Schema.</summary>
/// <returns>The JSON token.</returns>
public JToken ToDefaultJson(DefaultJsonDataGeneratorSettings? settings = default)
{
var generator = new DefaultJsonDataGenerator(settings);
return generator.Generate(this);
}

Expand Down

0 comments on commit 57e4917

Please sign in to comment.