Skip to content

[REQ][Java][Jersey2] Generate java method for each oneOf/anyOf #6892

Closed
@sebastien-rosset

Description

@sebastien-rosset

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

Currently, in the generated Java jersey2 library, a class is generated for every oneOf, anyOf schema in the OpenAPI document. Every oneOf/anyOf class inherits from AbstractOpenApiSchema. The AbstractOpenApiSchema.java class has the public Object getActualInstance() and Object getActualInstanceRecursively() methods which both return an Object. There is no type information in the return value besides being an Object.

IMO, there are two problems that could be improved:

  1. It's hard to tell what are the possible oneOf/anyOf classes returned by getActualInstance() (documentation issue)
  2. The developer must cast the return value of getActualInstance() and getActualInstanceRecursively()

Describe the solution you'd like

Suppose the OpenAPI document has oneOf: [X, Y, Z]. It would be helpful to:

  1. Generate documentation in the derived class indicating that getActualInstance can be a X, Y or Z. This could be done by overriding getActualInstance() which would just invoke super.getActualInstance(), and there would be generated method comments listing the possible type values.
  2. Add getter methods for each oneOf/anyOf (e.g. X, Y, Z). If we generate getX(), getY(), getZ(), the developer can invoke these generated methods instead of casting the return value. That way, the developers can no longer cast the return value to the wrong class. They can still invoke the wrong method, which presumably would cause a run time exception such as ClassCastException.

Describe alternatives you've considered

Currently, the developer must read the oneOf or anyOf sections of the OpenAPI document, find out what are the possible types. Then when calling getActualInstance(), the developer must cast the return value to the expected class.

Additional context

This would work well if the oneOf/anyOf schemas don't have recursion and if the oneOf/anyOf children are concrete. If there is recursion or if there are abstract classes involved, the developer may still have to cast the return value, but it would still be an improvement over what we have now.

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