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

Issue275 jsonobject causes argumentexception #275

Merged
merged 4 commits into from Jun 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions JsonSchema.Generation.Tests/ClientTests.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
using NUnit.Framework;
Expand Down Expand Up @@ -159,4 +161,27 @@ public void Issue272_UnhandledAttributeShouldBeIgnoredWhenOptimizing()

AssertEqual(expected, actual);
}

private class JsonObjectProp
{
public JsonObject ObjectProp { get; set; }
public JsonArray ArrayProp { get; set; }
public JsonValue ValueProp { get; set; }
}

[Test]
public void GenerateForPlanImpl()
{
JsonSchema expected = new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Properties(
("ObjectProp", new JsonSchemaBuilder().Type(SchemaValueType.Object)),
("ArrayProp", new JsonSchemaBuilder().Type(SchemaValueType.Array)),
("ValueProp", true)
);

JsonSchema actual = new JsonSchemaBuilder().FromType<JsonObjectProp>();

AssertEqual(expected, actual);
}
}
3 changes: 3 additions & 0 deletions JsonSchema.Generation/GeneratorRegistry.cs
Expand Up @@ -22,6 +22,9 @@ public static class GeneratorRegistry
new DateTimeSchemaGenerator(),
new GuidSchemaGenerator(),
new JsonPointerSchemaGenerator(),
new JsonArraySchemaGenerator(),
new JsonObjectSchemaGenerator(),
new JsonValueSchemaGenerator(),
new UriSchemaGenerator(),
// the dictionary ones are enumerable, so they need to come before the array one
new StringDictionarySchemaGenerator(),
Expand Down
18 changes: 18 additions & 0 deletions JsonSchema.Generation/Generators/JsonArraySchemaGenerator.cs
@@ -0,0 +1,18 @@
using System;
using System.Text.Json.Nodes;
using Json.Schema.Generation.Intents;

namespace Json.Schema.Generation.Generators;

internal class JsonArraySchemaGenerator : ISchemaGenerator
{
public bool Handles(Type type)
{
return type == typeof(JsonArray);
}

public void AddConstraints(SchemaGenerationContextBase context)
{
context.Intents.Add(new TypeIntent(SchemaValueType.Array));
}
}
18 changes: 18 additions & 0 deletions JsonSchema.Generation/Generators/JsonObjectSchemaGenerator.cs
@@ -0,0 +1,18 @@
using System;
using System.Text.Json.Nodes;
using Json.Schema.Generation.Intents;

namespace Json.Schema.Generation.Generators;

internal class JsonObjectSchemaGenerator : ISchemaGenerator
{
public bool Handles(Type type)
{
return type == typeof(JsonObject);
}

public void AddConstraints(SchemaGenerationContextBase context)
{
context.Intents.Add(new TypeIntent(SchemaValueType.Object));
}
}
17 changes: 17 additions & 0 deletions JsonSchema.Generation/Generators/JsonValueSchemaGenerator.cs
@@ -0,0 +1,17 @@
using System;
using System.Text.Json.Nodes;

namespace Json.Schema.Generation.Generators;

internal class JsonValueSchemaGenerator : ISchemaGenerator
{
public bool Handles(Type type)
{
return type == typeof(JsonValue);
}

public void AddConstraints(SchemaGenerationContextBase context)
{
// don't add constraints; any JSON value is fine here
}
}
4 changes: 2 additions & 2 deletions JsonSchema.Generation/JsonSchema.Generation.csproj
Expand Up @@ -15,8 +15,8 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<DocumentationFile>JsonSchema.Net.Generation.xml</DocumentationFile>
<LangVersion>latest</LangVersion>
<Version>2.2.0</Version>
<FileVersion>2.2.0.0</FileVersion>
<Version>2.2.1</Version>
<FileVersion>2.2.1.0</FileVersion>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand Down
2 changes: 2 additions & 0 deletions JsonSchema.Generation/SchemaGenerationContextBase.cs
Expand Up @@ -67,6 +67,8 @@ public JsonSchema Apply(JsonSchemaBuilder? builder = null)
if (ReferenceEquals(this, True)) return true;
if (ReferenceEquals(this, False)) return false;

if (!Intents.Any()) return true;

builder ??= new JsonSchemaBuilder();

foreach (var intent in Intents)
Expand Down
8 changes: 0 additions & 8 deletions JsonSchema.Generation/SchemaGenerationContextOptimizer.cs
Expand Up @@ -44,14 +44,6 @@ internal static void Optimize()
context.Intents.Clear();
context.Intents.Add(refIntent);
}
//else if (def.Hash == memberContext.BasedOn.Hash)
//{
// // this is always the case because type intents are added first
// // if there are any member attributes (e.g. List<int> with minimum 5)
// // then we won't enter into this block
// context.Intents.RemoveRange(0, memberContext.BasedOn.Intents.Count);
// context.Intents.Add(refIntent);
//}
}
}
if (ReferenceEquals(def, root)) continue;
Expand Down
@@ -1,4 +1,8 @@
# [2.1.0](https://github.com/gregsdennis/json-everything/pull/273)
# [2.1.1](https://github.com/gregsdennis/json-everything/pull/275)

[#274](https://github.com/gregsdennis/json-everything/issues/274) - Added support for `JsonObject`, `JsonArray`, and `JsonValue`.

# [2.2.0](https://github.com/gregsdennis/json-everything/pull/273)

[#272](https://github.com/gregsdennis/json-everything/issues/272) - Miscellaneous attributes can cause incorrect optimizations.

Expand Down
1 change: 1 addition & 0 deletions json-everything.net/wwwroot/md/schema-generation.md
Expand Up @@ -110,6 +110,7 @@ The generator will handle most common types:
- enumerations (mapped to strings)
- `Uri`
- `JsonPointer` (from [JsonPointer.Net](https://www.nuget.org/packages/JsonPointer.Net/))
- `JsonObject`, `JsonArray`, & `JsonValue` (from namespace `System.Text.Json.Nodes`)
- `Guid`
- `DateTime`
- collections (`IEnumerable<T>`)
Expand Down