Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union types don't automatically inherit Interface fields during code generation #888

Open
shawncruz opened this issue Jun 16, 2022 · 1 comment
Assignees

Comments

@shawncruz
Copy link

The following spec is valid:

{
    "interfaces": {
        "shape": {
            "fields": [
                { "name": "sides", "type":  "integer", "description": "Indicates the number of sides a shape has"}
            ]
        }
    },
    "unions": {
        "shape": {
            "interfaces": ["shape"],
            "discriminator": "type",
            "types": [
                { "type": "shape_circle", "discriminator_value": "circle"},
                { "type": "shape_square", "discriminator_value": "square"},
                { "type": "shape_triangle", "discriminator_value": "triangle"},
            ]        
        }
    },
    "models": {
        "shape_circle": {
            "description": "This is a circle.",
            "fields": [
                { "name": "radius",  "type": "decimal" }
            ]
        },
        "shape_square": {
            "description": "This is a square.",
            "fields": [
                { "name": "length",  "type": "decimal" }
                { "name": "height",  "type": "decimal" }
            ]
        },
        "shape_triangle": {
            "description": "This is a triangle.",
            "fields": [
                { "name": "triangle_type",  "type": "TriangleTypeEnum" }
            ]
        },
    }
}

However, when running the apibuilder command on this spec, the generated classes will extend the trait Shape, but not generate the overridden field for sides. The example of this output would look something like:

class Circle needs to be abstract. Missing implementation for:
  def sides: Integer // inherited from trait Shape
final case class Circle(radius: Decimal) extends Shape

The workaround for this has been to explicitly add the interface fields to the models fields array, however we should most likely consider this spec json invalid before we even get to code generation.

@mbryzek
Copy link
Collaborator

mbryzek commented Nov 27, 2023

@shawncruz this is by design. We introduced templates (experimental feature) which automatically adds the fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants