pwall567 / json-kotlin-schema-codegen

Code generation for JSON Schema (Draft 07)
MIT License
77 stars 14 forks source link

Unable to generate classes from opds2 spec #25

Open asm0dey opened 1 year ago

asm0dey commented 1 year ago

With the following code

val codeGenerator = CodeGenerator()
codeGenerator.baseDirectoryName = "output/directory"
codeGenerator.basePackageName = "com.example"
codeGenerator.generate(URI("https://raw.githubusercontent.com/opds-community/drafts/master/schema/feed.schema.json"))

I get the following exception:

Exception in thread "main" net.pwall.json.pointer.JSONPointerException: Recursive $ref - 
    at net.pwall.json.schema.parser.Parser.parseSchema(Parser.kt:175)
    at net.pwall.json.schema.parser.Parser.parseRef(Parser.kt:327)
        …

I do understand that maybe there are loops somewhere in refs, but I don't understand where. It would be very helpful to add the information about the loop into the error

pwall567 commented 1 year ago

Hi – you see that dash at the end of the "Exception" line? There is in fact a JSON Pointer following that dash, but the JSON Pointer for the root of an object is an empty string! I realise that's not very helpful, and I'll improve that error message.

The fact that the pointer is a root reference is a clue to the problem; it means that a schema contains a reference to the root of the schema object itself. Looking at your file feed.schema.json I see that it includes (by reference) the schema file https://readium.org/webpub-manifest/schema/subcollection.schema.json, and that file contains several recursive references to itself. Similarly, the file https://readium.org/webpub-manifest/schema/link.schema.json contains multiple references to itself.

The code generator currently does not handle recursive references, and in any case, the JSON Schema specification does not allow the use of $ref recursively; Draft 2019-09 defined $recursiveRef for this purpose, and Draft 2020-12 replaces this with $dynamicRef.

I can't help you with your recursive references, but at least I plan to improve the error reporting!

-Peter

asm0dey commented 1 year ago

Thank you! Yes, the only thing I'm asking for is to display the actual loop: it would help to report the actual issue in the schema to developers

asm0dey commented 1 year ago

I mean sure , it would be cool if you supported recursive references, but if it's impossible for now - better messages would be cool too!

asm0dey commented 1 year ago

@pwall567 I think that you might be wrong that the JSON Schema specification prohibits recursive referencing: http://json-schema.org/understanding-json-schema/structuring.html#recursion

pwall567 commented 1 year ago

@asm0dey - that document is an introductory guide, written for JSON Schema Draft 7. Draft 2019-09 changed the handling of recursion (as is alluded to in that document) and Draft 2020-12 made further changes (see Schema References).

My implementation is something of a hybrid – it was written mostly to the Draft 2019-09 standard, but because it does not include some of the features of the later drafts (such as vocabularies), the team managing the standard asked me to label it as implementing Draft 7. The Draft 7 specification clearly mentions recursion, so it's a fair criticism to say that my implementation is not complete in that regard, but I had planned to cover recursion using the $recursiveRef and $dynamicRef constructs.

I have a new version in development that will cover all of Draft 2020-12, but that is a long-term project, and I can't offer any completion date at this stage.

milux commented 1 month ago

@asm0dey If that still has any relevance for you, you may try out my PR: https://github.com/pwall567/json-kotlin-schema-codegen/pull/43 Feedback welcome!

asm0dey commented 1 month ago

It is an interesting excercise indeed, but not too simple when the jar is not published :)

milux commented 1 month ago

mvn install is your friend. :wink:

milux commented 1 month ago

Here you go, if you don't mind using jitpack as additional artifact source: https://jitpack.io/#milux/json-kotlin-schema-codegen/0.108.2