I'm defining an API using swagger. I have a certain operation that should return something like this.
{"isDeviceLoaded":true,"loadedDeviceProperties": {"deviceType":"Gadget","deviceProp1":0,"deviceLoadedConfigurations":[{"width":0,"length":0,"distanceToTopLeft":0}]}}
The problem is when the "isDeviceLoaded:false" I don't want to return all the properties, so in this case the return value should be only.
{"isDeviceLoaded":false}
I defined my model as below following this Swagger - Specify Optional Object Property or Multiple Responses
DeviceMediaStatus:
type: "object"
required: ["isDeviceLoaded"]
properties:
isDeviceLoaded:
type: "boolean"
loadedDeviceProperties:
type: "object"
$ref: "#/definitions/LoadedDeviceProperties"
but when in my code I try to return only the isDeviceLoaded property I get a response validation failed. This is what I'm doing in my JS and "object" has the data.
if(object.LoadedDeviceStatus.isDeviceLoaded){
response = object.LoadedDeviceStatus;
res.status(200).send(response);
}else{
response = object.LoadedDeviceStatus.isDeviceLoaded;
res.status(200).send(response);
}