fix: generation fails when schemas have an $Id field #241
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The generator generates file names using the $id value as its preferred option. This causes problems with code generation because the IDs typically contain unwanted characters that may change the path of the files or aren't allowed by the OS.
Since the x-schema-parser-id is what we tend to use to identify schemas and the name of the schema is either the parser ID or the json/yaml key of the schema, we would run into unnecessary confusion.
In short, when a schema with $id is present, the generator tries to name the file the $id value, which is identified by a different, independent x-schema-parser-id typically derived from a sometimes different key name.
We already prefer to use the x-schema-parser-id to identify schemas but sometimes use the key name to help, so the $id was causing us to not write the modelClass correctly.
Solution
The implemented fix is to remove the $id from the document before feeding it to the generator as part of the generate:before hook since $id isn't being used in the code generator anyway.
Theory
If $id would need to remain intact...
...the fix needs to be much more complicated. There are two big problems.
Problem 1: The code generator doesn't and shouldn't know how the generator names files. The implementation shouldn't matter, but since the generator favours naming files based on $id, it'll be common for the name to be manipulated to become valid. For example, changing 'http://example.com/something' to 'http-example.com-something'. In order to fix this in the generate:after hook, we need to know how the generator comes up with the names which smells bad.
This could also be a problem with the current naming strategy without $id, but it seems very unlikely.
Problem 2: Mismatch of x-schema-parser-id, the file name generated from $id, and the key name. There will be times when we need to translate the $id into the schema's x-schema-parser-id so that we know which schema we're talking about. We can do it early once, for now, but may to do it in other places later on.
For example...
Theoretical solution
Related issue(s)
Resolves #208