如何基于模式或基于数据库结构生成自动swagger API doc(yaml)格式?

时间:2018-02-23 06:57:04

标签: node.js api express swagger

有没有办法根据我的架构或数据库结构自动生成 swagger API 文档,而不是手动键入get,put,delete,post和yaml中的所有这些东西,这是swagger API文档它只是自动生成。 谢谢,你可以问我关于创建脚手架和东西​​的事情。

api doc(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
  });
};

1 个答案:

答案 0 :(得分:0)

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

"尝试来自strongloop的环回项目,它可以连接到数据库表并从中创建模型(公开为swagger)。"

我认为这是Loopback链接:

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

让我知道它是否适合你;)