1

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);
  }
Community
  • 1
  • 1
acostela
  • 2,597
  • 3
  • 33
  • 50
  • Who's throwing the response validation error? What is res? – Charlie Feb 29 '16 at 16:39
  • Can you please post the error you are getting on the response? Also can you post more of your JS code? Thanks. – Mike Feb 29 '16 at 18:52
  • 1
    Sorry for the late answer. I solved it. The problem was with an npm package that was missing. Thank you very much! – acostela Mar 01 '16 at 07:16

0 Answers0