Skip to content

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

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

Open
inaldt opened this issue May 20, 2023 · 0 comments
Open

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

inaldt opened this issue May 20, 2023 · 0 comments

Comments

@inaldt
Copy link

inaldt commented May 20, 2023

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

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

No branches or pull requests

1 participant