2

I'm using Swagger with OAS3, since I need anyOf support. I have an API call that can take one of 2 possible schemas, an account or an address. The schema works well in Swagger: it shows, and validates. But the example value shows only the first schema. I created an examples array, per the documentation, but I have no idea where to add it:

const accountSchema = {
  description: 'schema for adding a new account',
  type: 'object',
  properties: {
    account: {
      type: 'object',
      properties: {
        userId: {type: 'number'},
        platformId: {type: 'number'},
        name: {type: 'string'},
        key: {type: 'string'},
        secret: {type: 'string'},
        test: {type: 'boolean'},
      },
      required: ['userId', 'platformId', 'name', 'key', 'secret']
    }
  },
};

const ethereumAddressSchema = {
  description: 'schema for adding a new ethereum address',
  type: 'object',
  properties: {
    ethereum: {
      type: 'object',
      properties: {
        userId: {type: 'number'},
        name: {type: 'string'},
        address: {type: 'string'},
        chainId: {type: 'number'},
      },
      required: ['userId','name', 'address']
    }
  }
};

const examples = [
  {
    account: {
      "userId": 0,
      "platformId": 0,
      "name": "string",
      "key": "string",
      "secret": "string",
      "test": true
    },
  },
  {
    ethereum: {
      "userId": 0,
      "address": '0xfffffffffffffff',
      "name": "string",
      "chainId": 1,
    }
  }
];

const body = {
  anyOf: [accountSchema, ethereumAddressSchema]
};

const response = {
  type: 'object',
  properties: {
    accountId: {type: 'number'},
    reason: {type: 'string'}
  },
  required: []
};

const addAccountSchema = {
  description: 'Add a new account',
  tags: ['account'],
  produces: ['application/json'],
  summary: 'Add a new account',
  body,
  response: {
    200: {
      description: 'Account request valid',
      ...response
    },
    404: {
      description: 'Account request parameters not found',
      ...response
    },
    422: {
      description: 'Account request invalid',
      ...response
    }
  }
};

module.exports = addAccountSchema;

Where should I add the examples array, or is there a better way to show the user the 2 possible schemas in the UI?

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • 1) What library do you use to convert this code into an OpenAPI file? 2) Multiple examples are supported on the media type level, i.e. in request body and response definitions - see [Adding Examples](https://swagger.io/docs/specification/adding-examples/). 3) You seem to have a mix of OAS3 syntax (`anyOf`) and OAS2 syntax (`produces`). Make sure you're actually using OAS3. – Helen Jul 01 '21 at 21:54
  • Thanks @Helen: I'm using fastify-swagger. I looked at that page, but still wasn't sure where in my JSON the examples get put into - tried different things, and none worked. Not sure about mixing OAS2 and 3 - what have I been doing wrong? What replaces `produces`? – Traveling Tech Guy Jul 02 '21 at 21:14

0 Answers0