Skip to content

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

@shawncruz

Description

@shawncruz

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions