-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #917 from RSuter/master
Release v9.13.20
- Loading branch information
Showing
13 changed files
with
155 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -6,11 +6,22 @@ | |
// <author>Rico Suter, [email protected]</author> | ||
//----------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace NJsonSchema.CodeGeneration.TypeScript | ||
{ | ||
/// <summary>Converts the default value to a TypeScript identifier.</summary> | ||
public class TypeScriptValueGenerator : ValueGeneratorBase | ||
{ | ||
private readonly List<string> _formatCompatibleWithString = new List<string>() | ||
{ | ||
JsonFormatStrings.Uri, | ||
JsonFormatStrings.Guid, | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
JsonFormatStrings.Uuid | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
}; | ||
|
||
/// <summary>Initializes a new instance of the <see cref="TypeScriptValueGenerator"/> class.</summary> | ||
/// <param name="settings">The settings.</param> | ||
public TypeScriptValueGenerator(TypeScriptGeneratorSettings settings) | ||
|
@@ -31,6 +42,11 @@ public override string GetDefaultValue(JsonSchema4 schema, bool allowsNull, stri | |
var value = base.GetDefaultValue(schema, allowsNull, targetType, typeNameHint, useSchemaDefault, typeResolver); | ||
if (value == null) | ||
{ | ||
if (schema.Type.HasFlag(JsonObjectType.String) && _formatCompatibleWithString.Contains(schema.Format)) | ||
{ | ||
return "\"" + ConversionUtilities.ConvertToStringLiteral(value.ToString()) + "\""; | ||
} | ||
|
||
var isOptional = (schema as JsonProperty)?.IsRequired == false; | ||
if (schema != null && allowsNull == false && isOptional == false) | ||
{ | ||
|
@@ -66,4 +82,4 @@ public override string GetNumericValue(JsonObjectType type, object value, string | |
return ConvertNumberToString(value); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
// <author>Rico Suter, [email protected]</author> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
|
@@ -15,7 +17,21 @@ namespace NJsonSchema.CodeGeneration | |
public abstract class ValueGeneratorBase | ||
{ | ||
private readonly CodeGeneratorSettingsBase _settings; | ||
|
||
private readonly List<string> _formatNotCompatibleWithString = new List<string>() | ||
{ | ||
JsonFormatStrings.Date, | ||
JsonFormatStrings.DateTime, | ||
JsonFormatStrings.Time, | ||
JsonFormatStrings.TimeSpan, | ||
JsonFormatStrings.Uri, | ||
JsonFormatStrings.Guid, | ||
JsonFormatStrings.Byte, | ||
#pragma warning disable CS0618 // Type or member is obsolete | ||
JsonFormatStrings.Uuid, | ||
JsonFormatStrings.Base64, | ||
#pragma warning restore CS0618 // Type or member is obsolete | ||
}; | ||
|
||
/// <summary>Initializes a new instance of the <see cref="ValueGeneratorBase" /> class.</summary> | ||
/// <param name="settings">The settings.</param> | ||
protected ValueGeneratorBase(CodeGeneratorSettingsBase settings) | ||
|
@@ -41,7 +57,8 @@ public virtual string GetDefaultValue(JsonSchema4 schema, bool allowsNull, strin | |
return GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver); | ||
|
||
if (schema.Type.HasFlag(JsonObjectType.String)) | ||
return "\"" + ConversionUtilities.ConvertToStringLiteral(schema.Default.ToString()) + "\""; | ||
return GetStringValue(schema.Type, schema.Default, schema.Format); | ||
|
||
if (schema.Type.HasFlag(JsonObjectType.Boolean)) | ||
return schema.Default.ToString().ToLowerInvariant(); | ||
if (schema.Type.HasFlag(JsonObjectType.Integer) || | ||
|
@@ -51,6 +68,14 @@ public virtual string GetDefaultValue(JsonSchema4 schema, bool allowsNull, strin | |
return null; | ||
} | ||
|
||
private string GetStringValue(JsonObjectType type, object value, string format) | ||
{ | ||
if(!_formatNotCompatibleWithString.Contains(format)) | ||
return "\"" + ConversionUtilities.ConvertToStringLiteral(value.ToString()) + "\""; | ||
else | ||
return null; | ||
} | ||
|
||
/// <summary>Converts the default value to a number literal. </summary> | ||
/// <param name="type">The JSON type.</param> | ||
/// <param name="value">The value to convert.</param> | ||
|
@@ -117,4 +142,4 @@ protected string ConvertNumberToString(object value) | |
return null; | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="ItemsCanBeNullAttribute.cs" company="NJsonSchema"> | ||
// Copyright (c) Rico Suter. All rights reserved. | ||
// </copyright> | ||
// <license>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</license> | ||
// <author>Rico Suter, [email protected]</author> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
|
||
namespace NJsonSchema.Annotations | ||
{ | ||
/// <summary>Annotation to specify that array items are nullable.</summary> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.Field)] | ||
public class ItemsCanBeNullAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains 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 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