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
2 changed files with 75 additions and 0 deletions

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();
}
}