fix: avro schema union types generate object type #250
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
When specifying multiple (union) types in an avro schema, we used to blindly use the x-schema-parser-id as the type, but a union type is technically a schema itself, so the x-schema-parser-id, instead of being a type, is an anonymous schema.
Union type example
Generated code sample
Solution
Similar to the function in all.js, getMessagePayloadType, fixType has an order of which location has the correct type (There's a chance there's some code duplication here, but we dont have tests that cover the necessary branches so I'm holding off on it for now).
The order of preference for getting the type is something like this from most preferred (type) to least (parser-id):
There's also cases for enums and arrays but I'm omitting them in this description.
This works because each value in our type field (the union)...
... is a schema itself and the generator assigns these tiny schemas to an anyOf() keyword. This makes sense in all cases where type isn't specified, so the user can still have an anyOf() that they defined themselves and we still need to say it's an object. The only potential difference is between json schema and avro schema. In avro, we must say it's an Object and we can't be smart about choosing for the user. In json schema, we could potentially guess (iirc).
Additionally, the generated toString() method now calls toString() on any Object type within the toString().
Related issue(s)
Resolves #247