mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 14:40:53 +00:00
add getActualInstanceRecursively (#6636)
This commit is contained in:
parent
b729e1d723
commit
951aa7a9d9
@ -52,6 +52,25 @@ public abstract class AbstractOpenApiSchema {
|
||||
*/
|
||||
public void setActualInstance(Object instance) {this.instance = instance;}
|
||||
|
||||
/**
|
||||
* Get the instant recursively when the schemas defined in oneOf/anyof happen to be oneOf/anyOf schema as well
|
||||
*
|
||||
* @return an instance of the actual schema/object
|
||||
*/
|
||||
public Object getActualInstanceRecursively() {
|
||||
return getActualInstanceRecursively(this);
|
||||
}
|
||||
|
||||
private Object getActualInstanceRecursively(AbstractOpenApiSchema object) {
|
||||
if (object.getActualInstance() == null) {
|
||||
return null;
|
||||
} else if (object.getActualInstance() instanceof AbstractOpenApiSchema) {
|
||||
return getActualInstanceRecursively((AbstractOpenApiSchema)object.getActualInstance());
|
||||
} else {
|
||||
return object.getActualInstance();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema type (e.g. anyOf, oneOf)
|
||||
*
|
||||
|
@ -63,6 +63,25 @@ public abstract class AbstractOpenApiSchema {
|
||||
*/
|
||||
public void setActualInstance(Object instance) {this.instance = instance;}
|
||||
|
||||
/**
|
||||
* Get the instant recursively when the schemas defined in oneOf/anyof happen to be oneOf/anyOf schema as well
|
||||
*
|
||||
* @return an instance of the actual schema/object
|
||||
*/
|
||||
public Object getActualInstanceRecursively() {
|
||||
return getActualInstanceRecursively(this);
|
||||
}
|
||||
|
||||
private Object getActualInstanceRecursively(AbstractOpenApiSchema object) {
|
||||
if (object.getActualInstance() == null) {
|
||||
return null;
|
||||
} else if (object.getActualInstance() instanceof AbstractOpenApiSchema) {
|
||||
return getActualInstanceRecursively((AbstractOpenApiSchema)object.getActualInstance());
|
||||
} else {
|
||||
return object.getActualInstance();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema type (e.g. anyOf, oneOf)
|
||||
*
|
||||
|
@ -63,6 +63,25 @@ public abstract class AbstractOpenApiSchema {
|
||||
*/
|
||||
public void setActualInstance(Object instance) {this.instance = instance;}
|
||||
|
||||
/**
|
||||
* Get the instant recursively when the schemas defined in oneOf/anyof happen to be oneOf/anyOf schema as well
|
||||
*
|
||||
* @return an instance of the actual schema/object
|
||||
*/
|
||||
public Object getActualInstanceRecursively() {
|
||||
return getActualInstanceRecursively(this);
|
||||
}
|
||||
|
||||
private Object getActualInstanceRecursively(AbstractOpenApiSchema object) {
|
||||
if (object.getActualInstance() == null) {
|
||||
return null;
|
||||
} else if (object.getActualInstance() instanceof AbstractOpenApiSchema) {
|
||||
return getActualInstanceRecursively((AbstractOpenApiSchema)object.getActualInstance());
|
||||
} else {
|
||||
return object.getActualInstance();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema type (e.g. anyOf, oneOf)
|
||||
*
|
||||
|
@ -45,12 +45,29 @@ public class MammalTest {
|
||||
Zebra z = new Zebra();
|
||||
z.setType(Zebra.TypeEnum.MOUNTAIN);
|
||||
z.setClassName("zebra");
|
||||
|
||||
m.setActualInstance(z);
|
||||
|
||||
Assert.assertEquals(JSON.getDefault().getMapper().writeValueAsString(m), "{\"type\":\"mountain\",\"className\":\"zebra\"}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Model tests for getActualInstanceRecursively
|
||||
*/
|
||||
@Test
|
||||
public void testGetActualInstanceRecursively() throws Exception {
|
||||
Mammal m = new Mammal();
|
||||
Pig p = new Pig();
|
||||
DanishPig dp = new DanishPig();
|
||||
dp.setClassName("danish_pig");
|
||||
p.setActualInstance(dp);
|
||||
m.setActualInstance(p);
|
||||
Assert.assertTrue(m.getActualInstanceRecursively() instanceof DanishPig);
|
||||
|
||||
Pig p2 = new Pig();
|
||||
m.setActualInstance(p2);
|
||||
Assert.assertEquals(m.getActualInstanceRecursively(), null);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the property 'hasBaleen'
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user