0

我将 Swagger 与 OAS3 一起使用,因为我需要anyOf支持。我有一个 API 调用可以采用两种可能的模式之一,一个account或一个address. 该模式在 Swagger 中运行良好:它显示并验证。但该example值仅显示第一个模式。我根据文档创建了一个examples数组,但我不知道在哪里添加它:

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;

我应该在哪里添加examples数组,还是有更好的方法向用户显示 UI 中的 2 个可能的模式?

4

0 回答 0