【问题标题】:How to generate auto swagger API doc (yaml) format based on schema or based on database structure?如何基于模式或基于数据库结构生成自动 swagger API doc (yaml) 格式?
【发布时间】:2018-03-08 17:32:56
【问题描述】:

有什么方法可以根据我的架构或数据库结构自动生成 swagger API 文档,而不是在 yaml 中手动输入 get、put、delete、post 和所有这些东西swagger API docs 它只会自动生成。 谢谢,你可以问我任何关于创建脚手架和东西​​的问题。

api 文档(yaml 格式)

swagger: "2.0"
info:
  version: 1.0.0
  title: practice
  description: practice yo scuffolding
basePath: /api/v1
tags:
  - name: Examples
    description: Simple example endpoints
  - name: Person
    description: Simple person endpoints
  - name: Account
    description: Simple person endpoints
  - name: Specification
    description: The swagger API specification

consumes:
  - application/x-www-form-urlencoded
  - application/form-data
  - application/json
produces:
  - application/json

definitions:
  ExampleBody:
    type: object
    title: example
    required:
      - name
    properties:
      name:
        type: string
        description: The example name
  PersonBodyUpdate:
    type: object
    title: person
    required:
      oneOf:
        - Name
        - Position
        - Gender
    properties:
      Name:
        type: string
        description: The Person name
      Position:
        type: string
        description: The Person position
      Gender:
        type: string
        description: The Person gender
  PersonBodyPost:
    type: object
    title: person
    required: true
      - Name
      - Position
      - Gender
    properties:
      Name:
        type: string
        description: The Person name
      Position:
        type: string
        description: The Person position
      Gender:
        type: string
        description: The Person gender      
  AccountBody:
    type: object
    title: account
    required:
      - Username
      - Password
      - Person_id
    properties:
      Username:
        type: string
        description: The Account name
      Password:
        type: string
        description: The Account position
      Person_id:  
        type: integer
        description: The Account Person_id 
  LoginBody:
    type: object
    title: login
    required: true
      - Username
      - Password
    properties:
      Username:
        type: string
        description: The Account Username
      Password:
        type: string
        description: The Account Password                      
paths:
  /examples:
    get:
      tags:
        - Examples
      description: Fetch all examples
      responses:
        200:
          description: Returns all examples
    post:
      tags:
        - Examples
      description: Create a new example
      parameters:
        - name: example
          in: body
          description: number of items to skip
          required: true
          schema: 
            $ref: "#/definitions/ExampleBody"
      responses:
        200:
          description: Returns all examples

  /examples/{id}:
    get:
      tags:
        - Examples
      parameters:
        - name: id
          in: path
          required: true
          description: The id of the entity to retrieve
          type: integer
      responses:
        200:
          description: Return the example with the specified id
        404:
          description: Example not 

  /spec:
    get:
      tags:
        - Specification
      responses:
        200:
          description: Return the API specification

  /person:
    get:
      tags:
        - Person
      description: Fetch all person
      responses:
        200:
          description: "successful operation"
    post:
      tags:
        - Person 
      description: Create a new person
      parameters:
        - name: person
          in: body
          description: number of items to skip
          required: true
          schema: 
            $ref: "#/definitions/PersonBodyPost"
      responses:
        200:
          description: Create person

  /person/{id}:
    delete:
      tags:
        - Person
      parameters:
        - name: id
          in: path
          required: true
          description: The id of the entity to be delete
          type: integer
      responses:
        200:
          description: Return the products with the specified id
        404:
          description: Products not 4u2Dj7
    put:
      tags:
        - Person
      parameters:
        - name: id
          in: path
          required: true
          description: The id of the entity to be delete
          type: integer
        - name: person
          in: body
          description: number of items to skip
          required: true
          schema: 
            $ref: "#/definitions/PersonBodyUpdate"  
      responses:
        200:
          description: Return the products with the specified id
        404:
          description: Products not

  /account:
    get:
      tags:
        - Account
      description: Fetch all account
      responses:
        200:
          description: "successful operation"
    post:
      tags:
        - Account 
      description: Create a new account
      parameters:
        - name: account
          in: body
          description: number of items to skip
          required: true
          schema: 
            $ref: "#/definitions/AccountBody"
      responses:
        200:
          description: Create account

  /account/{id}:
    delete:
      tags:
        - Account
      parameters:
        - name: id
          in: path
          required: true
          description: The id of the entity to be delete
          type: integer
      responses:
        200:
          description: Return the account with the specified id
        404:
          description: Products not 4u2Dj7
    put:
      tags:
        - Account
      parameters:
        - name: id
          in: path
          required: true
          description: The id of the entity to be delete
          type: integer
        - name: account
          in: body
          description: number of items to skip
          required: true
          schema: 
            $ref: "#/definitions/AccountBody"  
      responses:
        200:
          description: Return the account with the specified id
        404:
          description: Account not Found

  /account/login:
    get: 
      tags:
        - Account 
      description: login an account
      security:
        - basicAuth:[]  
      responses:
        200:
          description: Create account            

架构:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('account', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    Username: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    Password: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    status: {
      type: DataTypes.STRING(45),
      allowNull: true
    },
    Person_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      references: {
        model: 'person',
        key: 'id'
      }
    }
  }, {
    tableName: 'account',
    timestamps: false
  });
};

【问题讨论】:

    标签: node.js api express swagger


    【解决方案1】:

    我在 Github 上看到了同样的问题,它得到了这个答案:

    “试试 strongloop 的 loopback 项目,它可以连接到数据库表并从中创建模型(暴露为 swagger)。”

    我认为是 Loopback 链接:

    https://loopback.io/doc/en/lb2/Swagger-generator.html

    让我知道它是否对你有用;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多