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

Outputting a JSchema with another keyword next to the $ref keyword produces and invalid schema #286

Open
sfwester opened this issue Nov 12, 2021 · 1 comment

Comments

@sfwester
Copy link

From 2019-09 spec section 8.2.4.1

The "$ref" keyword is an applicator that is used to reference a
statically identified schema. Its results are the results of the
referenced schema. [[CREF9: Note that this definition of how the
results are determined means that other keywords can appear alongside
of "$ref" in the same schema object. ]]

I tried this out using Newtonsoft.Json.Schema 3.0.14 dotnet fiddle of the code below

using System;
using Newtonsoft.Json.Schema;

namespace Tinker
{
    class Program
    {
        static void Main(string[] args)
        {
            string refSchema = @"{
                ""type"": ""string""
            }";

            JSchemaPreloadedResolver resolver = new JSchemaPreloadedResolver();
            resolver.Add(new Uri("refSchema", UriKind.RelativeOrAbsolute), refSchema);

            JSchema mainSchema = JSchema.Parse(@"{
                ""$schema"": ""http://json-schema.org/draft-2019-09/schema"",
                ""title"": ""A Title"",
                ""$ref"": ""refSchema""
            }", resolver);

            Console.WriteLine(mainSchema);

            JSchema.Parse(mainSchema.ToString());
        }
    }
}

This program outputs

{
  "$schema": "http://json-schema.org/draft-2019-09/schema",
  "title": "A Title",
  "$ref": "#/$ref"
}

and then throws

Unhandled exception. Newtonsoft.Json.Schema.JSchemaReaderException: Could not resolve schema reference '#/$ref'. Path '', line 1, position 1.

I would expect it to produce a valid schema that can be reparsed.

It may be worth noting that the expected schema is up to the application developer and the schema author to agree on. See section 7.7.1.1 Distinguishing Among Multiple Values

The application programmer and the schema author need to agree on the
usage.

So, ideally, the library would provide some options for how to deal with multiple values if the referenced schema also included the title keyword. e.g., prefer local or prefer referenced.

@omaryrajaa
Copy link

This is still encountered in 3.0.15
When are we expecting solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants