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

JsonSchema.GenerateCode fails with "This schema is not in a supported form." with no additional details #523

Closed
1 task done
NaridaL opened this issue Sep 11, 2023 · 11 comments
Labels
bug Something isn't working pkg:schema-codegen

Comments

@NaridaL
Copy link

NaridaL commented Sep 11, 2023

Nuget Package

JsonSchema.Net.CodeGeneration

Package Version

0.1.1

Operating System

Windows

.Net Target (if relevant, please specify the version in the description)

.Net Framework

A clear and concise description of the bug

I'm evaluating using JsonSchema.Net.CodeGeneration for code generation, but it is failing with this exception:

Json.Schema.CodeGeneration.UnsupportedSchemaException: This schema is not in a supported form.
    at Json.Schema.CodeGeneration.Model.ModelGenerator.GenerateCodeModel(JsonSchema schema, EvaluationOptions options, GenerationCache cache)
   at Json.Schema.CodeGeneration.CodeGenExtensions.GenerateCode(JsonSchema schema, ICodeWriter codeWriter, EvaluationOptions options)
   at Ess.Security.FenceDataModel.UnitTests.GenerateSchemaTests.GenerateSchema() in C:\ws\Infra.Security.FenceDeploymentAutomation\src\Ess.Security.FenceDataModel.UnitTests\GenerateSchema.cs:line 18

Please add test code or steps to reproduce the behavior

This is an example failing schema, but that it is failing isn't the main issue, it's the fact that the exception doesn't give any details as to why it has failed.


        var schema = JsonSchema.FromFile("../../../../My.schema.json",
            new JsonSerializerOptions { ReadCommentHandling = JsonCommentHandling.Skip });
        var generatedCode = schema.GenerateCode(CodeWriters.CSharp);
        File.WriteAllText("Fence.cs", generatedCode);
My.schema.json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  //"additionalProperties": false,
  "title": "Fence",
  "type": "object",
  "required": [
    "$schema",
    "scid"
  ],
  "properties": {
    "$schema": {
      "pattern": "/Fence\\.schema\\.json$"
    },
    "scid": {
      "type": "string",
      "pattern": "^\\d{7,8}$"
    }
  }
}

What did you expect?

A specific exception message with details to the failure

Is there any other information you'd like to share regarding this bug?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@NaridaL NaridaL added the bug Something isn't working label Sep 11, 2023
@gregsdennis
Copy link
Owner

gregsdennis commented Sep 11, 2023

Hello. Please read the documentation to see what's supported.

The system validates your schema with some schemas of it's own to make sure that it's supported. The validation results should be in the Data dictionary of the exception.

I say "should," but it doesn't look like I actually did that that. I can add it.

@gregsdennis
Copy link
Owner

gregsdennis commented Sep 11, 2023

Looking at your schema, the $schema property needs a type.

@NaridaL
Copy link
Author

NaridaL commented Sep 11, 2023

Hello. Please read the documentation to see what's supported.

The system validates your schema with some schemas of it's own to make sure that it's supported. The validation results should be in the Data dictionary of the exception.

I say "should," but it doesn't look like I actually did that that. I can add it.

Thanks for the quick response? Can we include a textual representation of the validation errors as part of the exception message? That will provide immediate feedback to the next user :-)

@gregsdennis
Copy link
Owner

Sorry, that would require a certain level of analysis of the results that we (JSON Schema) haven't figured out. The best bet would be to give you the results and publish the schemas I'm using internally.

@NaridaL
Copy link
Author

NaridaL commented Sep 11, 2023

I don't follow. According to the docs, one of the output formats is List, which is just a list of errors. Is it not possible to concat those errors (along with their location path)?

@gregsdennis
Copy link
Owner

The problem is that there are potentially a lot of errors that could appear and not all of them are super useful for any particular case, especially since the schema's I'm using are coordinated by a oneOf. If you submit a schema that should be an enum but don't get it exactly right, you only want errors for the enum case. But you'll get errors from all of the cases, and that's confusing because you won't know which ones to address.

See also this discussion.

The best option is for me to just put the validation results directly in the exception, and let you debug.

@gregsdennis
Copy link
Owner

I'll also add some more technical documentation regarding what's supported.

@gregsdennis
Copy link
Owner

gregsdennis commented Sep 12, 2023

Please see https://docs.json-everything.net/schema/codegen/patterns/. I've not yet updated the library.

@NaridaL
Copy link
Author

NaridaL commented Sep 12, 2023

OK, I've copied out the schema being used for validation so I could output the errors, and I see what you mean about the output.

To give more context, I'm evaluating this library to generate C# model classes from our existing schema. We're currently (partially) using NJsonSchema, but it is missing some features we need and doesn't seems to be actively maintained (cf RicoSuter/NJsonSchema#1605 ).

Unfortunately, the full schema is 300 lines and I'm currently getting 400 errors. I spent 10 minutes looking through the errors to the find out the actual cause but had no success.

My suggestion here would be to add a custom property (eg "x-json-everything-oneof-disambiguator": "type"), which would just mark one error if the type property is missing, or use it to pick which oneof schema to verify.

@gregsdennis
Copy link
Owner

My suggestion here would be to add a custom property (eg "x-json-everything-oneof-disambiguator": "type"), which would just mark one error if the type property is missing, or use it to pick which oneof schema to verify.

I'm not sure what you mean here. Where does this property go? How would it help?

If you want to send me your output, you can do so in a DM in Slack. There's a link in the readme.

@gregsdennis
Copy link
Owner

gregsdennis commented Oct 2, 2023

Ugh... typos thwarting automation!

image

Anyway, I've added the validation results to the exception, and docs detailing the schemas I'm using should be up soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:schema-codegen
Projects
None yet
Development

No branches or pull requests

2 participants