php-schema2class icon indicating copy to clipboard operation
php-schema2class copied to clipboard

Support JSON schema with references?

Open SvenRtbg opened this issue 5 years ago • 3 comments

I've looked at this library and would say that it is the most sophisticated of the ones I found. But it still has some features missing. One of them is the (for me) unexpected limitation to YAML format, combined with the inability to resolve references ($ref) in a JSON-formatted schema.

You are using justinrainbow/jsonschema already. I tried to make it work for schema loading and monkey-patched something that works for me. Are you interested in integrating this change upstream? I would prepare a PR then. It looks like a relatively small change: The SchemaLoader has to be changed to accept JSON, and SchemaToClass has to recursively resolve references once they are encontered.

I don't know if this change will allow to keep YAML support, as the schema reading uses the justinrainbow library now, which is expecting JSON.

Let me know what you think!

SvenRtbg avatar May 23 '19 14:05 SvenRtbg

Thanks for reaching out and for the kind feedback.

One of them is the (for me) unexpected limitation to YAML format

The reason for that is simply that YAML was all I needed at the time, nothing more. I‘d be happy to accept PRs for integrating JSON support, though. Same goes for resolving references.

martin-helmich avatar May 23 '19 18:05 martin-helmich

I'm still on this, but things are a little bit more complicated.

Even though I like the idea that the generated class can validate based on the schema, a schema itself can be an infinite definition because it can reference itself in a loop. Just think of tree structures. Any tree itself cannot be infinite, but the definition will have a "node" connected to other "node" again and again. This is the reason why the JsonSchema validator cannot return "the fully resolved schema" before validation, but will constantly resolve references whenever it is needed.

I also don't like the idea that the main class validates the full schema, and any sub class will again validate for it's own part. But I'd consider this a minor issue.

SvenRtbg avatar May 31 '19 08:05 SvenRtbg

Would it help if you didn't do the validation inside the class itself? It's quite the boilerplate anyway, if you have lots of schemas. You could build the classes up from the input data in a factory. That factory only has to validate the input once and can then go recursively, safely.

hcomnetworkers avatar Jul 06 '19 14:07 hcomnetworkers