【问题标题】:Swagger definition from model来自模型的 Swagger 定义
【发布时间】:2019-11-06 09:19:47
【问题描述】:

我有这个数据模型:

"use strict";

const mongoose = require("mongoose");
const Schema = mongoose.Schema;


let ProviderSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  emailTemplate: {
    type: Schema.ObjectId,
    ref: "EmailTemplate",
    required: false
  }
});


let ProductsSchema = new Schema({
    brand: {
        type: Schema.ObjectId,
        ref: "Brand",
        required: true 
    },
    providers: {
        type: [ProviderSchema],
        required: false
    }
});


let ListSchema = new Schema({
  code: {
    type: String,
    unique: true,
    trim: true,
    required: true
  },
  products: {
    type: [ProductsSchema],
    default: [],
    required: true
  }
}, { collection: 'list' });


module.exports = mongoose.model("List", ListSchema);

但是当我尝试构建 swagger 定义时,我遇到了问题。如何定义模型的“产品”属性? 我尝试使用类型数组来执行此操作,但它在招摇验证中给了我一个错误,我认为是因为它构建得很糟糕。我是招摇的新手,定义这个很复杂

这是我的 .yaml

required:
  - code
properties:
  code:
    type: string
  products:
    type: array
        items:
            $ref: '#/definitions/Brand'

【问题讨论】:

  • 您是手动编写 YAML 还是从代码生成它?
  • 我是手动写的,不知道怎么用代码生成,谢谢

标签: node.js mongoose yaml swagger


【解决方案1】:

缩进在 YAML 中很重要。确保itemstype: array 处于同一级别。

required:
  - code
properties:
  code:
    type: string
  products:
    type: array
    items:      # <-------
      $ref: '#/definitions/Brand'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 2017-02-19
    • 2021-04-18
    • 2021-02-22
    相关资源
    最近更新 更多