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

Allow calling Draft4Support.Enable() multiple times #11

Merged
merged 10 commits into from
Jul 8, 2024
12 changes: 12 additions & 0 deletions Graeae.Models.Tests/ApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Graeae.Models.SchemaDraft4;

namespace Graeae.Models.Tests;

public class ApiTests
{
[Test]
public void InitializingAgainDoesNotThrow()
{
Draft4Support.Enable();
}
}
6 changes: 3 additions & 3 deletions Graeae.Models/Graeae.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<PackageReleaseNotes>$([System.IO.File]::ReadAllText("$(MSBuildProjectDirectory)/../RELEASE_NOTES.md"))</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>Graeae.Models.xml</DocumentationFile>
<Version>0.3.1.1</Version>
<FileVersion>0.3.1.1</FileVersion>
<AssemblyVersion>0.3.0.0</AssemblyVersion>
<Version>0.3.2</Version>
<FileVersion>0.3.2</FileVersion>
<AssemblyVersion>0.3.2.0</AssemblyVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
4 changes: 0 additions & 4 deletions Graeae.Models/Graeae.Models.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 21 additions & 18 deletions Graeae.Models/SchemaDraft4/Draft4Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Graeae.Models.SchemaDraft4;
/// </summary>
public static class Draft4Support
{
private static JsonSchema? _draft4MetaSchema;

/// <summary>
/// Defines a JSON Schema draft 4 spec version.
/// </summary>
Expand All @@ -28,24 +30,11 @@ public static class Draft4Support
/// <summary>
/// Defines the JSON Schema draft 4 meta-schema.
/// </summary>
/// <remarks>
/// This property is initialized by the <see cref="Enable"/> method and
/// will be null if accessed before that method is called.
/// </remarks>
public static JsonSchema Draft4MetaSchema { get; private set; } = null!;
public static JsonSchema Draft4MetaSchema => _draft4MetaSchema ??= InitializeDraft4MetaSchema();

/// <summary>
/// Enables support for OpenAPI v3.0 and JSON Schema draft 4.
/// </summary>
public static void Enable()
private static JsonSchema InitializeDraft4MetaSchema()
{
SchemaKeywordRegistry.Register<Draft4ExclusiveMaximumKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<Draft4ExclusiveMinimumKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<Draft4IdKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<NullableKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<Draft4TypeKeyword>(GraeaeSerializerContext.Default);

Draft4MetaSchema = new JsonSchemaBuilder()
JsonSchema draft4MetaSchema = new JsonSchemaBuilder()
.OasId(Draft4MetaSchemaUri)
.Schema(Draft4MetaSchemaUri)
.Description("Core schema meta-schema")
Expand Down Expand Up @@ -176,9 +165,23 @@ public static void Enable()
("exclusiveMinimum", new[] { "minimum" })
)
.Default(new JsonObject());
Draft4MetaSchema.BaseUri = new Uri(Draft4MetaSchemaUri);

draft4MetaSchema.BaseUri = new Uri(Draft4MetaSchemaUri);
// This allows draft 4 to be used as a meta-schema.
SchemaRegistry.RegisterNewSpecVersion(Draft4MetaSchema.BaseUri, Draft4Version);
SchemaRegistry.RegisterNewSpecVersion(draft4MetaSchema.BaseUri, Draft4Version);
return draft4MetaSchema;
}

/// <summary>
/// Enables support for OpenAPI v3.0 and JSON Schema draft 4.
/// </summary>
public static void Enable()
{
SchemaKeywordRegistry.Register<Draft4ExclusiveMaximumKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<Draft4ExclusiveMinimumKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<Draft4IdKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<NullableKeyword>(GraeaeSerializerContext.Default);
SchemaKeywordRegistry.Register<Draft4TypeKeyword>(GraeaeSerializerContext.Default);

SchemaRegistry.Global.Register(Draft4MetaSchema);
}
Expand Down
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[0.3.2](https://github.com/gregsdennis/Graeae/pull/11)

- Fixed a bug that Draft4Support.Enable() crashed when called multiple times.

[0.3.1](https://github.com/gregsdennis/Graeae/pull/10)

_v0.3.1.1 is a re-publish to include these release notes._
Expand Down
Loading