Skip to content

Commit

Permalink
Migrate to 10.9.0 (#10)
Browse files Browse the repository at this point in the history
* Update README.md

* Handle single quote in properties names (RicoSuter#1574)

* added abstract schema checking to CSharpValueGenerator GetDefaultValue (RicoSuter#1570)

Co-authored-by: Gergo Vandor <gergo.vandor.dev@outlook.com>

* Recursive sample generation (RicoSuter#1561)

* Add tests that show generator doesnt handle definition re-use/recursion

* Allow definition re-use/recursion in SampleJsonDataGenerator

Adds a MaxRecursionLevel to SampleJsonDataGeneratorSettings

* Add test of recursion level

* Fix race condition in GetName (RicoSuter#1571)

* Update Namotion.Reflection

* v11.1.0

* Revert "v11.1.0"

This reverts commit 1015b01.

* Post-merge adjustments

---------

Co-authored-by: Rico Suter <mail@rsuter.com>
Co-authored-by: kal <35899782+kalilistic@users.noreply.github.com>
Co-authored-by: Gergo Vandor <vandor.gergoo@gmail.com>
Co-authored-by: Gergo Vandor <gergo.vandor.dev@outlook.com>
Co-authored-by: Flemming Madsen <mail@leflings.dk>
Co-authored-by: SeongChan Lee <foriequal@gmail.com>
Co-authored-by: Rico Suter <rico.suter@buhlergroup.com>
  • Loading branch information
8 people committed Nov 16, 2023
1 parent d83a41e commit 43a0e26
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 69 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
[![Azure DevOps](https://img.shields.io/azure-devops/build/rsuter/NJsonSchema/17/master.svg)](https://rsuter.visualstudio.com/NJsonSchema/_build?definitionId=17)
[![Nuget](https://img.shields.io/nuget/v/NJsonSchema.svg)](https://www.nuget.org/packages?q=NJsonSchema)
[![MyGet](https://img.shields.io/myget/njsonschema/v/NJsonSchema.svg?label=preview%20nuget)](https://www.myget.org/feed/Packages/njsonschema)
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-1dce73.svg)](https://gitter.im/NJsonSchema/NJsonSchema)
[![Discord](https://img.shields.io/badge/Discord-join%20chat-1dce73.svg)](https://discord.gg/4x48JjUT)
[![Discord](https://img.shields.io/badge/Discord-join%20chat-1dce73.svg)](https://discord.gg/BxQNy25WF6)
[![StackOverflow](https://img.shields.io/badge/questions-on%20StackOverflow-orange.svg?style=flat)](http://stackoverflow.com/questions/tagged/njsonschema)
[![Wiki](https://img.shields.io/badge/docs-in%20wiki-orange.svg?style=flat)](https://github.com/RicoSuter/njsonschema/wiki)
[![Apimundo](https://img.shields.io/badge/Architecture-Apimundo-728199.svg)](https://apimundo.com/organizations/github/projects/ricosuter?tab=repositories)
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<VersionPrefix>10.8.0</VersionPrefix>
<VersionPrefix>10.9.0</VersionPrefix>

<Authors>Rico Suter</Authors>
<Copyright>Copyright © Rico Suter, 2022</Copyright>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public virtual string Generate(JsonSchemaProperty property)
{
return ConversionUtilities.ConvertToUpperCamelCase(property.Name
.Replace("\"", string.Empty)
.Replace("'", string.Empty)
.Replace("@", string.Empty)
.Replace("?", string.Empty)
.Replace("$", string.Empty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override string GetDefaultValue(JsonSchema schema, bool allowsNull, strin
? _settings.ArrayInstanceType + targetType.Substring(_settings.ArrayType.Length)
: targetType;

return $"new {targetType}()";
return schema.IsAbstract ? null : $"new {targetType}()";
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<EmbeddedResource Include="Templates\*.liquid" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NJsonSchema.CodeGeneration\NJsonSchema.CodeGeneration.csproj" />
Expand Down
27 changes: 27 additions & 0 deletions src/NJsonSchema.CodeGeneration.Tests/DefaultValueGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,32 @@ public void When_schema_has_default_value_of_enum_it_is_generated_in_CSharp_and_
Assert.Equal("Ns.MyEnum.bar", csharpValue);
Assert.Equal("MyEnum.bar", typescriptValue);
}

[Fact]
public void When_schema_has_required_abstract_class_it_generates_no_default_value_for_in_CSharp_and_TypeScript()
{
//// Arrange
var csharpSettings = new CSharpGeneratorSettings();
var csharpGenerator = new CSharpValueGenerator(csharpSettings);
var csharpTypeResolver = new CSharpTypeResolver(csharpSettings);

//// Act
var schema = new JsonSchema()
{
Type = JsonObjectType.Object,
IsAbstract = true
};

var typescriptSettings = new TypeScriptGeneratorSettings();
var typescriptGenerator = new TypeScriptValueGenerator(typescriptSettings);
var typescriptTypeResolver = new TypeScriptTypeResolver(typescriptSettings);

var csharpValue = csharpGenerator.GetDefaultValue(schema, false, "BaseClass", "BaseClass", true, csharpTypeResolver);
var typescriptValue = typescriptGenerator.GetDefaultValue(schema, false, "BaseClass", "BaseClass", true, typescriptTypeResolver);

//// Assert
Assert.Null(csharpValue);
Assert.Null(typescriptValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<EmbeddedResource Include="Templates\File.ParseDateOnly.liquid" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NJsonSchema.CodeGeneration\NJsonSchema.CodeGeneration.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fluid.Core" Version="2.2.15" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Text.Encodings.Web" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
Expand Down
164 changes: 164 additions & 0 deletions src/NJsonSchema.Tests/Generation/SampleJsonDataGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,170 @@ public async Task PropertyWithIntegerMinimumDefiniton()
Assert.Equal(1, testJson.SelectToken("body.numberContent.value").Value<int>());
}

[Fact]
public async Task SchemaWithRecursiveDefinition()
{
//// Arrange
var data = @"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""title"": ""test schema"",
""type"": ""object"",
""required"": [
""body"", ""footer""
],
""properties"": {
""body"": {
""$ref"": ""#/definitions/body""
},
""footer"": {
""$ref"": ""#/definitions/numberContent""
}
},
""definitions"": {
""body"": {
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""numberContent"": {
""$ref"": ""#/definitions/numberContent""
}
}
},
""numberContent"": {
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""value"": {
""type"": ""number"",
""maximum"": 5.00001,
""minimum"": 1.000012
},
""data"": {
""$ref"": ""#/definitions/body""
}
}
}
}
}";
var generator = new SampleJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);

//// Assert
var footerToken = testJson.SelectToken("body.numberContent.data.numberContent.value");
Assert.NotNull(footerToken);

var validationResult = schema.Validate(testJson);
Assert.NotNull(validationResult);
Assert.Equal(1.000012, testJson.SelectToken("footer.value").Value<double>());
Assert.True(validationResult.Count > 0); // It is expected to fail validating the recursive properties (because of max recursion level)
}

[Fact]
public async Task GeneratorAdheresToMaxRecursionLevel()
{
//// Arrange
var data = @"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""title"": ""test schema"",
""type"": ""object"",
""required"": [
""body"", ""footer""
],
""properties"": {
""body"": {
""$ref"": ""#/definitions/body""
}
},
""definitions"": {
""body"": {
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""text"": { ""type"": ""string"", ""enum"": [""my_string""] },
""body"": {
""$ref"": ""#/definitions/body""
}
}
}
}
}";
var generator = new SampleJsonDataGenerator(new SampleJsonDataGeneratorSettings() { MaxRecursionLevel = 2 });
var schema = await JsonSchema.FromJsonAsync(data);
//// Act
var testJson = generator.Generate(schema);

//// Assert
var secondBodyToken = testJson.SelectToken("body.body");
Assert.NotNull(secondBodyToken);

var thirdBodyToken = testJson.SelectToken("body.body.body") as JValue;
Assert.NotNull(thirdBodyToken);
Assert.Equal(JTokenType.Null, thirdBodyToken.Type);

var validationResult = schema.Validate(testJson);
Assert.NotNull(validationResult);
Assert.True(validationResult.Count > 0); // It is expected to fail validating the recursive properties (because of max recursion level)
}

[Fact]
public async Task SchemaWithDefinitionUseMultipleTimes()
{
//// Arrange
var data = @"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""title"": ""test schema"",
""type"": ""object"",
""required"": [
""body"", ""footer""
],
""properties"": {
""body"": {
""$ref"": ""#/definitions/body""
},
""footer"": {
""$ref"": ""#/definitions/numberContent""
}
},
""definitions"": {
""body"": {
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""numberContent"": {
""$ref"": ""#/definitions/numberContent""
}
}
},
""numberContent"": {
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""value"": {
""type"": ""number"",
""maximum"": 5.00001,
""minimum"": 1.000012
}
}
}
}
}";
var generator = new SampleJsonDataGenerator();
var schema = await JsonSchema.FromJsonAsync(data);

//// Act
var testJson = generator.Generate(schema);

//// Assert
var footerToken = testJson.SelectToken("footer.value");
Assert.NotNull(footerToken);

var validationResult = schema.Validate(testJson);
Assert.NotNull(validationResult);
Assert.Equal(0, validationResult.Count);
Assert.Equal(1.000012, testJson.SelectToken("body.numberContent.value").Value<double>());
}

[Fact]
public async Task PropertyWithFloatMinimumDefinition()
Expand Down
Loading

0 comments on commit 43a0e26

Please sign in to comment.