diff --git a/src/Json.Schema.ToDotNet/JsonSchemaExtensions.cs b/src/Json.Schema.ToDotNet/JsonSchemaExtensions.cs index d7bb4cd1..75680803 100644 --- a/src/Json.Schema.ToDotNet/JsonSchemaExtensions.cs +++ b/src/Json.Schema.ToDotNet/JsonSchemaExtensions.cs @@ -19,6 +19,11 @@ internal static bool IsUri(this JsonSchema schema) schema.IsStringWithFormat(FormatAttributes.UriReference); } + internal static bool IsUuid(this JsonSchema schema) + { + return schema.IsStringWithFormat(FormatAttributes.Uuid); + } + private static bool IsStringWithFormat(this JsonSchema schema, string format) { return diff --git a/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs b/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs index 1c36d483..1ee1da6e 100644 --- a/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs +++ b/src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs @@ -245,6 +245,13 @@ private void AddPropertyInfoFromPropertySchema( initializationKind = InitializationKind.Uri; type = MakeNamedType("System.Uri", out namespaceName); } + else if (propertySchema.IsUuid()) + { + comparisonKind = ComparisonKind.OperatorEquals; + hashKind = HashKind.ScalarValueType; + initializationKind = InitializationKind.SimpleAssign; + type = MakeNamedType("System.Guid", out namespaceName); + } else if (propertySchema.ShouldBeDictionary(_typeName, schemaPropertyName, _hintDictionary, out DictionaryHint dictionaryHint)) { comparisonKind = ComparisonKind.Dictionary; diff --git a/src/Json.Schema.UnitTests/ReaderWriter.cs b/src/Json.Schema.UnitTests/ReaderWriter.cs index 7e107b9f..7fdb53e4 100644 --- a/src/Json.Schema.UnitTests/ReaderWriter.cs +++ b/src/Json.Schema.UnitTests/ReaderWriter.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; -using Newtonsoft.Json.Linq; namespace Microsoft.Json.Schema.UnitTests { @@ -244,6 +243,24 @@ internal static class ReaderWriter } }, + new object[] + { + "Uuid", + new JsonSchema + { + Type = new SchemaType[] { SchemaType.Object }, + + Properties = new Dictionary + { + ["uuid"] = new JsonSchema + { + Type = new SchemaType[] { SchemaType.String }, + Format = FormatAttributes.Uuid + } + } + } + }, + new object[] { "AdditionalPropertiesBoolean", diff --git a/src/Json.Schema.UnitTests/TestData/Uuid.schema.json b/src/Json.Schema.UnitTests/TestData/Uuid.schema.json new file mode 100644 index 00000000..40c7e214 --- /dev/null +++ b/src/Json.Schema.UnitTests/TestData/Uuid.schema.json @@ -0,0 +1,9 @@ +{ + "type": "object", + "properties": { + "uuid": { + "type": "string", + "format": "uuid" + } + } +} \ No newline at end of file diff --git a/src/Json.Schema/FormatAttributes.cs b/src/Json.Schema/FormatAttributes.cs index 5461e153..670ebdef 100644 --- a/src/Json.Schema/FormatAttributes.cs +++ b/src/Json.Schema/FormatAttributes.cs @@ -31,5 +31,11 @@ public static class FormatAttributes /// URI references as defined by RFC 3986. /// public const string UriReference = "uri-reference"; + + /// + /// Format attribute specifying that the string instance must be a valid + /// UUID as defined by RFC 4122. + /// + public const string Uuid = "uuid"; } } diff --git a/src/ReleaseHistory.md b/src/ReleaseHistory.md index 0840b327..14f41a3d 100644 --- a/src/ReleaseHistory.md +++ b/src/ReleaseHistory.md @@ -2,6 +2,7 @@ ## **Unreleased** * Loosen Newtonsoft.JSON minimum version requirement from v13.0.1 to v9.0.1 [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/9.0.1). [#157](https://github.com/microsoft/jschema/pull/157) +* FEATURE: Add support for JSON Schema type `uuid`. [#132](https://github.com/microsoft/jschema/pull/132) * FEATURE: Add new option for specifying .NET type to express Json integers: `--generate-json-integer-as = int | long | biginteger | auto` with a default of `int`. [#158](https://github.com/microsoft/jschema/pull/158) ## **1.1.5** [Pointer](https://www.nuget.org/packages/Microsoft.Json.Pointer/1.1.5) | [Schema](https://www.nuget.org/packages/Microsoft.Json.Schema/1.1.4)| [Schema.ToDotNet](https://www.nuget.org/packages/Microsoft.Json.Schema.ToDotNet/1.1.4)| [Schema.Validation](https://www.nuget.org/packages/Microsoft.Json.Schema.Validation/1.1.5)