forked from loafle/openapi-generator-original
add null type support to oneof/anyof java jersey2 (#6269)
This commit is contained in:
@@ -606,7 +606,6 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
HashMap<String, Object> h = (HashMap<String, Object>) o;
|
||||
CodegenModel m = (CodegenModel) h.get("model");
|
||||
modelMaps.put(m.classname, m);
|
||||
|
||||
}
|
||||
|
||||
// check if return type is oneOf/anyeOf model
|
||||
@@ -809,6 +808,18 @@ public class JavaClientCodegen extends AbstractJavaCodegen
|
||||
//List<String> impl = (List<String>) cm.getVendorExtensions().get("x-implements");
|
||||
if (JERSEY2.equals(getLibrary())) {
|
||||
cm.getVendorExtensions().put("x-implements", new ArrayList<String>());
|
||||
|
||||
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.contains("ModelNull")) {
|
||||
// if oneOf contains "null" type
|
||||
cm.isNullable = true;
|
||||
cm.oneOf.remove("ModelNull");
|
||||
}
|
||||
|
||||
if (cm.anyOf != null && !cm.anyOf.isEmpty() && cm.anyOf.contains("ModelNull")) {
|
||||
// if anyOf contains "null" type
|
||||
cm.isNullable = true;
|
||||
cm.anyOf.remove("ModelNull");
|
||||
}
|
||||
}
|
||||
if (this.parcelableModel) {
|
||||
((ArrayList<String>) cm.getVendorExtensions().get("x-implements")).add("Parcelable");
|
||||
|
||||
@@ -16,11 +16,15 @@ public abstract class AbstractOpenApiSchema {
|
||||
// store the actual instance of the schema/object
|
||||
private Object instance;
|
||||
|
||||
// is nullable
|
||||
private Boolean isNullable;
|
||||
|
||||
// schema type (e.g. oneOf, anyOf)
|
||||
private final String schemaType;
|
||||
|
||||
public AbstractOpenApiSchema(String schemaType) {
|
||||
public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
|
||||
this.schemaType = schemaType;
|
||||
this.isNullable = isNullable;
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -52,4 +56,17 @@ public abstract class AbstractOpenApiSchema {
|
||||
public String getSchemaType() {
|
||||
return schemaType;
|
||||
}
|
||||
|
||||
/***
|
||||
* Is nullalble
|
||||
*
|
||||
* @return true if it's nullable
|
||||
*/
|
||||
public Boolean isNullable() {
|
||||
if (Boolean.TRUE.equals(isNullable)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -848,6 +848,15 @@ public class ApiClient {
|
||||
int matchCounter = 0;
|
||||
ArrayList<String> matchSchemas = new ArrayList<>();
|
||||
|
||||
if (schema.isNullable()) {
|
||||
response.bufferEntity();
|
||||
if ("{}".equals(String.valueOf(response.readEntity(String.class))) ||
|
||||
"".equals(String.valueOf(response.readEntity(String.class)))) {
|
||||
// schema is nullable and the response body is {} or empty string
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, GenericType> entry : schema.getSchemas().entrySet()) {
|
||||
String schemaName = entry.getKey();
|
||||
GenericType schemaType = entry.getValue();
|
||||
|
||||
@@ -11,7 +11,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
||||
public final static Map<String, GenericType> schemas = new HashMap<String, GenericType>();
|
||||
|
||||
public {{classname}}() {
|
||||
super("anyOf");
|
||||
super("anyOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
|
||||
}
|
||||
|
||||
static {
|
||||
@@ -25,4 +25,23 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
||||
public Map<String, GenericType> getSchemas() {
|
||||
return {{classname}}.schemas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setActualInstance(Object instance) {
|
||||
{{#isNulalble}}
|
||||
if (instance == null) {
|
||||
super.setActualInstance(instance);
|
||||
return;
|
||||
}
|
||||
|
||||
{{/isNulalble}}
|
||||
{{#anyOf}}
|
||||
if (instance instanceof {{{.}}}) {
|
||||
super.setActualInstance(instance);
|
||||
return;
|
||||
}
|
||||
|
||||
{{/anyOf}}
|
||||
throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
||||
public final static Map<String, GenericType> schemas = new HashMap<String, GenericType>();
|
||||
|
||||
public {{classname}}() {
|
||||
super("oneOf");
|
||||
super("oneOf", {{#isNullable}}Boolean.TRUE{{/isNullable}}{{^isNullable}}Boolean.FALSE{{/isNullable}});
|
||||
}
|
||||
|
||||
static {
|
||||
@@ -28,6 +28,13 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
||||
|
||||
@Override
|
||||
public void setActualInstance(Object instance) {
|
||||
{{#isNulalble}}
|
||||
if (instance == null) {
|
||||
super.setActualInstance(instance);
|
||||
return;
|
||||
}
|
||||
|
||||
{{/isNulalble}}
|
||||
{{#oneOf}}
|
||||
if (instance instanceof {{{.}}}) {
|
||||
super.setActualInstance(instance);
|
||||
@@ -38,4 +45,6 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
|
||||
throw new RuntimeException("Invalid instance type. Must be {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -760,6 +760,15 @@ public class ApiClient {
|
||||
int matchCounter = 0;
|
||||
ArrayList<String> matchSchemas = new ArrayList<>();
|
||||
|
||||
if (schema.isNullable()) {
|
||||
response.bufferEntity();
|
||||
if ("{}".equals(String.valueOf(response.readEntity(String.class))) ||
|
||||
"".equals(String.valueOf(response.readEntity(String.class)))) {
|
||||
// schema is nullable and the response body is {} or empty string
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, GenericType> entry : schema.getSchemas().entrySet()) {
|
||||
String schemaName = entry.getKey();
|
||||
GenericType schemaType = entry.getValue();
|
||||
|
||||
@@ -27,11 +27,15 @@ public abstract class AbstractOpenApiSchema {
|
||||
// store the actual instance of the schema/object
|
||||
private Object instance;
|
||||
|
||||
// is nullable
|
||||
private Boolean isNullable;
|
||||
|
||||
// schema type (e.g. oneOf, anyOf)
|
||||
private final String schemaType;
|
||||
|
||||
public AbstractOpenApiSchema(String schemaType) {
|
||||
public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
|
||||
this.schemaType = schemaType;
|
||||
this.isNullable = isNullable;
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -63,4 +67,17 @@ public abstract class AbstractOpenApiSchema {
|
||||
public String getSchemaType() {
|
||||
return schemaType;
|
||||
}
|
||||
|
||||
/***
|
||||
* Is nullalble
|
||||
*
|
||||
* @return true if it's nullable
|
||||
*/
|
||||
public Boolean isNullable() {
|
||||
if (Boolean.TRUE.equals(isNullable)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -761,6 +761,15 @@ public class ApiClient {
|
||||
int matchCounter = 0;
|
||||
ArrayList<String> matchSchemas = new ArrayList<>();
|
||||
|
||||
if (schema.isNullable()) {
|
||||
response.bufferEntity();
|
||||
if ("{}".equals(String.valueOf(response.readEntity(String.class))) ||
|
||||
"".equals(String.valueOf(response.readEntity(String.class)))) {
|
||||
// schema is nullable and the response body is {} or empty string
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, GenericType> entry : schema.getSchemas().entrySet()) {
|
||||
String schemaName = entry.getKey();
|
||||
GenericType schemaType = entry.getValue();
|
||||
|
||||
@@ -27,11 +27,15 @@ public abstract class AbstractOpenApiSchema {
|
||||
// store the actual instance of the schema/object
|
||||
private Object instance;
|
||||
|
||||
// is nullable
|
||||
private Boolean isNullable;
|
||||
|
||||
// schema type (e.g. oneOf, anyOf)
|
||||
private final String schemaType;
|
||||
|
||||
public AbstractOpenApiSchema(String schemaType) {
|
||||
public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
|
||||
this.schemaType = schemaType;
|
||||
this.isNullable = isNullable;
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -63,4 +67,17 @@ public abstract class AbstractOpenApiSchema {
|
||||
public String getSchemaType() {
|
||||
return schemaType;
|
||||
}
|
||||
|
||||
/***
|
||||
* Is nullalble
|
||||
*
|
||||
* @return true if it's nullable
|
||||
*/
|
||||
public Boolean isNullable() {
|
||||
if (Boolean.TRUE.equals(isNullable)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -761,6 +761,15 @@ public class ApiClient {
|
||||
int matchCounter = 0;
|
||||
ArrayList<String> matchSchemas = new ArrayList<>();
|
||||
|
||||
if (schema.isNullable()) {
|
||||
response.bufferEntity();
|
||||
if ("{}".equals(String.valueOf(response.readEntity(String.class))) ||
|
||||
"".equals(String.valueOf(response.readEntity(String.class)))) {
|
||||
// schema is nullable and the response body is {} or empty string
|
||||
return schema;
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, GenericType> entry : schema.getSchemas().entrySet()) {
|
||||
String schemaName = entry.getKey();
|
||||
GenericType schemaType = entry.getValue();
|
||||
|
||||
@@ -27,11 +27,15 @@ public abstract class AbstractOpenApiSchema {
|
||||
// store the actual instance of the schema/object
|
||||
private Object instance;
|
||||
|
||||
// is nullable
|
||||
private Boolean isNullable;
|
||||
|
||||
// schema type (e.g. oneOf, anyOf)
|
||||
private final String schemaType;
|
||||
|
||||
public AbstractOpenApiSchema(String schemaType) {
|
||||
public AbstractOpenApiSchema(String schemaType, Boolean isNullable) {
|
||||
this.schemaType = schemaType;
|
||||
this.isNullable = isNullable;
|
||||
}
|
||||
|
||||
/***
|
||||
@@ -63,4 +67,17 @@ public abstract class AbstractOpenApiSchema {
|
||||
public String getSchemaType() {
|
||||
return schemaType;
|
||||
}
|
||||
|
||||
/***
|
||||
* Is nullalble
|
||||
*
|
||||
* @return true if it's nullable
|
||||
*/
|
||||
public Boolean isNullable() {
|
||||
if (Boolean.TRUE.equals(isNullable)) {
|
||||
return Boolean.TRUE;
|
||||
} else {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user