Open Phil-Ba opened 5 years ago
Ok, problem seems to occur when description is missing. This seems to be working
types:
boolOrString:
description: test
type: string | boolean
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?
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 (https://github.com/phoenixnap/springmvc-raml-plugin/pull/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;
}
}
Let's go with the second approach - the one used in raml-for-jax-rs
.
Currently trying to use union types leads to a NullPointerException:
Example: