Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Uuids #132

Merged
merged 7 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Json.Schema.ToDotNet/JsonSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/Json.Schema.ToDotNet/PropertyInfoDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion src/Json.Schema.UnitTests/ReaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -244,6 +243,24 @@ internal static class ReaderWriter
}
},

new object[]
{
"Uuid",
new JsonSchema
{
Type = new SchemaType[] { SchemaType.Object },

Properties = new Dictionary<string, JsonSchema>
{
["uuid"] = new JsonSchema
{
Type = new SchemaType[] { SchemaType.String },
Format = FormatAttributes.Uuid
}
}
}
},

new object[]
{
"AdditionalPropertiesBoolean",
Expand Down
9 changes: 9 additions & 0 deletions src/Json.Schema.UnitTests/TestData/Uuid.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "object",
"properties": {
"uuid": {
"type": "string",
"format": "uuid"
}
}
}
6 changes: 6 additions & 0 deletions src/Json.Schema/FormatAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ public static class FormatAttributes
/// URI references as defined by RFC 3986.
/// </summary>
public const string UriReference = "uri-reference";

/// <summary>
/// Format attribute specifying that the string instance must be a valid
/// UUID as defined by RFC 4122.
/// </summary>
public const string Uuid = "uuid";
}
}
1 change: 1 addition & 0 deletions src/ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down