Union types with common fields
See original GitHub issueCurrently there’s no way in the api.json syntax to share common fields across all the model types of a Union: common fields need to be repeated in the definition of all model types. Then, the code generated consists always of an “empty” sealed base trait (/ interface) with all case classes that extend from it repeating all fields.
It’d be nice to have the ability to share fields across all the model types of a union and have the code generated with a shared base-trait/abstract class containing the common fields: this would make writing the code that handles these models much easier (avoid lots of type matching / casting when just need to access common fields).
Two possible ways of supporting this:
- add a
fields
property to the union declaration in api.json and have code generators reading from that to generate the base trait / abstract class for all the union types; - leave api.json syntax as it is and have the generators checking for common fields (all union types are models and all have a subset of fields with same name and type) and generating a base trait/class with them when found.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top Results From Across the Web
Handbook - Unions and Intersection Types - TypeScript
Unions with Common Fields. If we have a value that is a union type, we can only access members that are common to...
Read more >Learn TypeScript: Union Types Cheatsheet - Codecademy
As a result of supporting a union of multiple types, TypeScript allows you to access properties that are common among the member types...
Read more >Unions and interfaces - Apollo GraphQL Docs
Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types. Union type.
Read more >Accessing different properties in a typescript union type
I'm creating a function that handles objects from the database. I have two different data structures where the same property has a different ......
Read more >Union Types in TypeScript | Examples - eduCBA
3. Unions With Same Common Fields. Suppose if we have the variable value with same union type that time we can access only...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
related to https://github.com/apicollective/apibuilder/issues/441
I think the best path forward is to introduce a new top level entity named “interfaces” that would contain a list of fields.
Then you could add interfaces to each union type. Apidoc would then validate that each interface was in fact implemented by each of the types - allowing us to generate code with the interfaces applied (e.g. traits in scala).
The one option question is how to clearly indicate that a particular model should inherit the fields from an interface - do they get prepended? appended? simpler is to validate that the interface is in fact implemented but this doesn’t solve for the duplication in the spec.
implemented in https://github.com/apicollective/apibuilder/pull/826