question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Union types with common fields

See original GitHub issue

Currently 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:

  1. 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;
  2. 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mbryzekcommented, Sep 6, 2017

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.

0reactions
mbryzekcommented, May 22, 2020
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found