corvus-dotnet / Corvus.JsonSchema

Support for Json Schema validation and entity generation
Apache License 2.0
99 stars 9 forks source link

Unable to find the schema at... #410

Closed JamieKitson closed 2 weeks ago

JamieKitson commented 2 weeks ago

I am trying to use the generatejsonschematypes tool against this JSON schema. However, for virtually all paths as the --rootPath parameter I get back Unable to find the schema at... is there something wrong with this schema or is there a limitation of the tool or am I using it incorrectly?

Version: 3.1.0+7a3d554f50ee34dea70e2d95163f9d86d3155298

mwadams commented 2 weeks ago

Your schema contains a $id keyword for each of the definitions. This forces each of them into their own scope - so where you use your $ref keywords in the form #/definitions/[whatever] they are relative to the root of the current scope, not the root of the whole document. This is why everything with a $ref is apparently unresolvable (it actually is! 👍).

You can find out more about $id, and how it changes the base URI, in the json schema documentation

If you want to use $ids for everything, I recommend using URNs instead of fragments, and then explicitly use the URN. Or eliminate the IDs completely unless you really intend them all to be unique scopes.

For example, here's an example removing the unnecessary $ids.

application.json

Then I can generate code like this:

generatejsonschematypes.exe --rootPath '#/definitions/Agent' --outputPath ./Model --rootNamespace Feature410 .\application.json
JamieKitson commented 2 weeks ago

@mwadams Thanks very much.

JamieKitson commented 2 weeks ago

I'm now getting Unable to parse generated type:

C:\Users\jamie.kitson\Documents\Work\PlanX>generatejsonschematypes --rootNamespace JsonSchemaSample.Api application.json
Generating: ValueEntity
Unable to parse generated type: ValueEntity from location C:/Users/jamie.kitson/Documents/Work/PlanX/next#/definitions/PropertyType/anyOf/419/properties/value

C:\Users\jamie.kitson\Documents\Work\PlanX>generatejsonschematypes  --rootPath #\definitions\ApplicationType --rootNamespace JsonSchemaSample.Api application.json
Generating: ValueEntity
Generating: AnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfRequiredValueAndDescription
Unable to parse generated type: AnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfAnyOfRequiredValueAndDescription from location C:/Users/jamie.kitson/Documents/Work/PlanX/application.json#/definitions/ApplicationType/anyOf/4
mwadams commented 2 weeks ago

Ah! Now, that is a bug that I've just fixed in 4.0.0-preview, and will backport to 3.1 today.

You have large numbers of anonymous inline schema that overflow the maximum identifier name length. I'll push up a fix today.

mwadams commented 2 weeks ago

@JamieKitson:

I'm about to push a fix for this issue. It should be available in the next hour or so.

Also, note that if you are getting overlong and complicated names for some of your inline schema, you can use the extension keyword $corvusTypeName to set an explicit type name for them!

mwadams commented 2 weeks ago

Fixed in 3.1.1 and 4.0.0-preview.9