Skip to content

[REQ] Java: generate sealed interfaces for oneOf #15586

@inaldt

Description

@inaldt

Is your feature request related to a problem? Please describe.

The current handling of oneOf does not allow switch expressions on the generated type to be exhaustive.

Currently oneOf generates an interface:

Animal:
  oneOf:
    - $ref: '#/components/schemas/Cat'
    - $ref: '#/components/schemas/Dog'

generates

public interface Animal {...}
public class Cat implements Animal {...}
public class Dog implements Animal {...}

Switching over this requires a default clause:

switch (myAnimal) {
  case Dog d -> "dog"
  case Cat c -> "cat"
  default -> "There is no other class that implements Animal but I still need this clause here"
}

Avoiding default clauses in switch expressions is considered good practice as it allows the compiler to check for omissions.

Describe the solution you'd like

Animal:
  oneOf:
    - $ref: '#/components/schemas/Cat'
    - $ref: '#/components/schemas/Dog'

generates

public sealed interface Animal permits Cat, Dog {...}
public class Cat implements Animal {...}
public class Dog implements Animal {...}

Describe alternatives you've considered

The suggested feature is the canonical way to express sum types in Java, hence the canonical fit for oneOf.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions