This repository was archived by the owner on Jun 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
* BREAKING: .NET type to express Json integers now will be nullable if the property is not required and also without default. #167
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
190 changes: 117 additions & 73 deletions
190
src/Json.Schema.ToDotNet.UnitTests/DataModelGeneratorTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,9 @@ public class PropertyInfoDictionary : IReadOnlyDictionary<string, PropertyInfo> | |
| // <code>Location{}[]</code> is a dictionary property whose elements are arrays. | ||
| internal const string DictionaryMarker = "{}"; | ||
|
|
||
| internal const string IntType = "System.int"; | ||
| internal const string LongType = "System.long"; | ||
| internal const string GuidType = "System.Guid"; | ||
| internal const string BigIntegerType = "System.Numerics.BigInteger"; | ||
|
|
||
| /// <summary> | ||
|
|
@@ -234,6 +237,10 @@ private void AddPropertyInfoFromPropertySchema( | |
| bool isOfSchemaDefinedType = false; | ||
| int arrayRank = 0; | ||
|
|
||
| bool scalarValueTypeNullable = !isRequired && propertySchema.Default == null; | ||
| HashKind scalarValueTypeHashKind = | ||
| scalarValueTypeNullable ? HashKind.ScalarReferenceType : HashKind.ScalarValueType; | ||
|
|
||
| if (propertySchema.IsDateTime()) | ||
| { | ||
| comparisonKind = ComparisonKind.OperatorEquals; | ||
|
|
@@ -251,9 +258,9 @@ private void AddPropertyInfoFromPropertySchema( | |
| else if (propertySchema.IsUuid()) | ||
| { | ||
| comparisonKind = ComparisonKind.OperatorEquals; | ||
| hashKind = isRequired ? HashKind.ScalarValueType : HashKind.ScalarReferenceType; | ||
| hashKind = scalarValueTypeHashKind; | ||
| initializationKind = InitializationKind.SimpleAssign; | ||
| type = MakeNamedType(isRequired ? "System.Guid" : "System.Guid?", out namespaceName); | ||
| type = MakeNamedType(GuidType, out namespaceName, scalarValueTypeNullable); | ||
| } | ||
| else if (propertySchema.ShouldBeDictionary(_typeName, schemaPropertyName, _hintDictionary, out DictionaryHint dictionaryHint)) | ||
| { | ||
|
|
@@ -280,7 +287,7 @@ private void AddPropertyInfoFromPropertySchema( | |
| initializationKind = InitializationKind.SimpleAssign; | ||
| type = MakeNamedType(referencedEnumTypeName, out namespaceName); | ||
| } | ||
| else if (propertySchema.ShouldBeEnum(_typeName, schemaPropertyName, _hintDictionary, out EnumHint enumHint)) | ||
| else if (propertySchema.ShouldBeEnum(_typeName, schemaPropertyName, _hintDictionary, out EnumHint enumHint)) | ||
| { | ||
| comparisonKind = ComparisonKind.OperatorEquals; | ||
| hashKind = HashKind.ScalarValueType; | ||
|
|
@@ -313,14 +320,15 @@ private void AddPropertyInfoFromPropertySchema( | |
|
|
||
| case SchemaType.Integer: | ||
| comparisonKind = ComparisonKind.OperatorEquals; | ||
| hashKind = HashKind.ScalarValueType; | ||
| hashKind = scalarValueTypeHashKind; | ||
| initializationKind = InitializationKind.SimpleAssign; | ||
| type = MakeProperIntegerType(_generateJsonIntegerAs, | ||
| propertySchema.Minimum, propertySchema.ExclusiveMinimum, | ||
| propertySchema.Maximum, propertySchema.ExclusiveMaximum, | ||
| scalarValueTypeNullable, | ||
| out namespaceName); | ||
| break; | ||
|
|
||
| case SchemaType.String: | ||
| comparisonKind = ComparisonKind.OperatorEquals; | ||
| hashKind = HashKind.ScalarReferenceType; | ||
|
|
@@ -445,8 +453,13 @@ private TypeSyntax MakeObjectTypeFromReference(UriOrFragment reference, out stri | |
| return MakeNamedType(className, out namespaceName); | ||
| } | ||
|
|
||
| private TypeSyntax MakeNamedType(string typeName, out string namespaceName) | ||
| private TypeSyntax MakeNamedType(string typeName, out string namespaceName, bool nullable = false) | ||
| { | ||
| if (nullable) | ||
| { | ||
| typeName += "?"; | ||
| } | ||
|
|
||
| string unqualifiedTypeName = GetUnqualifiedTypeName(typeName, out namespaceName); | ||
|
|
||
| return SyntaxFactory.ParseTypeName(unqualifiedTypeName); | ||
|
|
@@ -630,7 +643,9 @@ private string GetUnqualifiedTypeName(string typeName, out string namespaceName) | |
| namespaceName = null; | ||
| } | ||
|
|
||
| return unqualifiedTypeName.ToPascalCase(); | ||
| namespaceName = namespaceName == "System" ? null : namespaceName; | ||
|
|
||
| return typeName.StartsWith("System.") ? unqualifiedTypeName : unqualifiedTypeName.ToPascalCase(); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| private void OnAdditionalTypeRequired(CodeGenHint hint, JsonSchema schema) | ||
|
|
@@ -687,20 +702,21 @@ private TypeSyntax GetConcreteType(string propertyName, string interfaceName) | |
| internal TypeSyntax MakeProperIntegerType(GenerateJsonIntegerOption generateJsonIntegerAs, | ||
| double? minimum, bool? exclusiveMinimum, | ||
| double? maximum, bool? exclusiveMaximum, | ||
| bool nullable, | ||
| out string namespaceName) | ||
| { | ||
| namespaceName = null; | ||
|
|
||
| switch (generateJsonIntegerAs) | ||
| { | ||
| case GenerateJsonIntegerOption.Long: | ||
| return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)); | ||
| return MakeNamedType(LongType, out namespaceName, nullable); | ||
| case GenerateJsonIntegerOption.BigInteger: | ||
| return MakeNamedType(BigIntegerType, out namespaceName); | ||
| return MakeNamedType(BigIntegerType, out namespaceName, nullable); | ||
| case GenerateJsonIntegerOption.Auto: | ||
| if (!minimum.HasValue || !maximum.HasValue) | ||
| { | ||
| return MakeNamedType(BigIntegerType, out namespaceName); | ||
| return MakeNamedType(BigIntegerType, out namespaceName, nullable); | ||
| } | ||
|
|
||
| if (exclusiveMinimum == true) | ||
|
|
@@ -720,12 +736,12 @@ internal TypeSyntax MakeProperIntegerType(GenerateJsonIntegerOption generateJson | |
|
|
||
| if (minimum.Value < int.MinValue || maximum.Value > int.MaxValue) | ||
| { | ||
| return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)); | ||
| return MakeNamedType(LongType, out namespaceName, nullable); | ||
| } | ||
|
|
||
| return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)); | ||
| return MakeNamedType(IntType, out namespaceName, nullable); | ||
| default: | ||
| return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.IntKeyword)); | ||
| return MakeNamedType(IntType, out namespaceName, nullable); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because
using System;is not required.