Skip to content
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

Union type support broken #289

Open
Phil-Ba opened this issue Oct 11, 2018 · 4 comments
Open

Union type support broken #289

Phil-Ba opened this issue Oct 11, 2018 · 4 comments
Labels
enhancement help wanted v2 Feature will be implemented in v2.0 only (master branch)

Comments

@Phil-Ba
Copy link

Phil-Ba commented Oct 11, 2018

Currently trying to use union types leads to a NullPointerException:

Caused by: java.lang.NullPointerException
at com.phoenixnap.oss.ramlplugin.raml2code.interpreters.UnionTypeInterpreter.getParent(UnionTypeInterpreter.java:69)
at com.phoenixnap.oss.ramlplugin.raml2code.interpreters.UnionTypeInterpreter.interpret(UnionTypeInterpreter.java:115)
at com.phoenixnap.oss.ramlplugin.raml2code.helpers.RamlTypeHelper.mapTypeToPojo(RamlTypeHelper.java:100)
at com.phoenixnap.oss.ramlplugin.raml2code.plugin.SpringMvcEndpointGeneratorMojo.generateUnreferencedObjects(Spring

Example:

types:
  boolOrString:
    displayName: test
    type: string | boolean
@Phil-Ba
Copy link
Author

Phil-Ba commented Oct 11, 2018

Ok, problem seems to occur when description is missing.
This seems to be working

types:
  boolOrString:
    description: test
    type: string | boolean

@stojsavljevic
Copy link
Contributor

Hi @Phil-Ba,

Thanks for reporting the issue.
I'm getting the same exception with both examples if the type is used as a query parameter.
Some other exceptions happen if the type is used as a request body.

TBH, I'm not sure how generated code should look like in this case.
What would you expect to get?
How should boolOrString POJO look like?

@lctncld
Copy link

lctncld commented Feb 6, 2019

I think there are at least two ways we can implement union types in Java. They are both not perfect but viable when type is used as a request or response body. However query parameter is a different story, I'm at loss there too.

So first approach is to create a field and an accessor for each type of the union. This is similar to how plugin works right now (see raml-with-union-types), but there are some problems when types extend other user-defined types (#298).

public class BoolOrString {

  private Boolean booleanType;
  private String stringType;

  public Boolean getBooleanType() {
    return booleanType;
  }

  public String getStringType() {
    return stringType;
  }
}

Another approach, currently implemented in mulesoft-labs/raml-for-jax-rs, is to use a single field and provide accessors with type checking and casting. I haven't tried this myself and not sure deserialization would work.

public class BoolOrString {

  private Object anyType;

  public Boolean getBooleanType() {
    if (!(anyType instanceof Boolean)) throw new IllegalStateException();
    return (Boolean) anyType;
  }

  public boolean isBooleanType() {
    return anyType instanceof Boolean;
  }

  public String getStringType() {
    if (!(anyType instanceof String)) throw new IllegalStateException();
    return (String) anyType;
  }

  public boolean isStringType() {
    return anyType instanceof String;
  }

}

@stojsavljevic
Copy link
Contributor

Let's go with the second approach - the one used in raml-for-jax-rs.

@stojsavljevic stojsavljevic added enhancement help wanted v2 Feature will be implemented in v2.0 only (master branch) and removed waiting for response labels Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted v2 Feature will be implemented in v2.0 only (master branch)
Projects
None yet
Development

No branches or pull requests

3 participants