add helper methods to anyOf in java models (#7130)

This commit is contained in:
William Cheng 2020-08-06 15:12:44 +08:00 committed by GitHub
parent 201ac77d0c
commit 4a7c4ac81d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 0 deletions

View File

@ -148,6 +148,14 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
return {{classname}}.schemas;
}
/**
* Set the instance that matches the anyOf child schema, check
* the instance parameter is valid against the anyOf child schemas:
* {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}
*
* It could be an instance of the 'anyOf' schemas.
* The anyOf child schemas may themselves be a composed schema (allOf, anyOf, anyOf).
*/
@Override
public void setActualInstance(Object instance) {
{{#isNullable}}
@ -166,4 +174,29 @@ public class {{classname}} extends AbstractOpenApiSchema{{#vendorExtensions.x-im
{{/anyOf}}
throw new RuntimeException("Invalid instance type. Must be {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}");
}
/**
* Get the actual instance, which can be the following:
* {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}
*
* @return The actual instance ({{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}})
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
{{#anyOf}}
/**
* Get the actual instance of `{{{.}}}`. If the actual instanct is not `{{{.}}}`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `{{{.}}}`
* @throws ClassCastException if the instance is not `{{{.}}}`
*/
public {{{.}}} get{{{.}}}() throws ClassCastException {
return ({{{.}}})super.getActualInstance();
}
{{/anyOf}}
}

View File

@ -153,6 +153,14 @@ public class GmFruit extends AbstractOpenApiSchema {
return GmFruit.schemas;
}
/**
* Set the instance that matches the anyOf child schema, check
* the instance parameter is valid against the anyOf child schemas:
* Apple, Banana
*
* It could be an instance of the 'anyOf' schemas.
* The anyOf child schemas may themselves be a composed schema (allOf, anyOf, anyOf).
*/
@Override
public void setActualInstance(Object instance) {
if (JSON.isInstanceOf(Apple.class, instance, new HashSet<Class<?>>())) {
@ -167,5 +175,39 @@ public class GmFruit extends AbstractOpenApiSchema {
throw new RuntimeException("Invalid instance type. Must be Apple, Banana");
}
/**
* Get the actual instance, which can be the following:
* Apple, Banana
*
* @return The actual instance (Apple, Banana)
*/
@Override
public Object getActualInstance() {
return super.getActualInstance();
}
/**
* Get the actual instance of `Apple`. If the actual instanct is not `Apple`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Apple`
* @throws ClassCastException if the instance is not `Apple`
*/
public Apple getApple() throws ClassCastException {
return (Apple)super.getActualInstance();
}
/**
* Get the actual instance of `Banana`. If the actual instanct is not `Banana`,
* the ClassCastException will be thrown.
*
* @return The actual instance of `Banana`
* @throws ClassCastException if the instance is not `Banana`
*/
public Banana getBanana() throws ClassCastException {
return (Banana)super.getActualInstance();
}
}