openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA] oneOf/anyOf multiple constructors with same erasure

Open Bethibande opened this issue 1 year ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using the okhttp-gson library, multiple constructors with the same erasure can be generated.

public ReposCreateDeploymentRequestPayload(List<Integer> o) {
    super("oneOf", Boolean.FALSE);
    setActualInstance(o);
}

public ReposCreateDeploymentRequestPayload(List<String> o) {
    super("oneOf", Boolean.FALSE);
    setActualInstance(o);
}

This leads to compiler errors since these constructors are identical. Gradle logs: https://gist.github.com/Bethibande/9582795e2e1fd47695019d8f1c685a8b

openapi-generator version

latest/master

Suggest a fix

This is the current constructor generation of the okhttp-gson oneof model template

{{#oneOf}}
public {{classname}}({{{.}}} o) {
    super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
    setActualInstance(o);
}

{{/oneOf}}

Change it to:

public {{classname}}(Object o) {
    super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
    setActualInstance(o);
}

The setActualInstance method also always takes Object as a parameter, it also validates the given object, so this should be fine, this also shouldn't break any existing code. In this case, it might also be a good idea to add some documentation about the types of parameters the constructor actually expects.

Bethibande avatar May 01 '24 12:05 Bethibande