在Swagger中,我的对象架构未显示

发布于 2025-02-12 09:39:29 字数 4957 浏览 2 评论 0原文

我正在Nodejs项目(Express Framework)工作 用Swagger-JSDOC和Swagger-UI-Express

我想输出一个模式和Swagger文档页面的示例 我在评论中添加了一个架构:

    /**
 * @swagger
 * components:
 *  schemas:
 *      Brand:
 *          type: object
 *          required:
 *              - name
 *          properties:
 *              id:
 *                  type: integer
... etc.

但是没有显示: screen1

我的链接github: https://github.com/kreotiff-dev/teststore/tree/changes_30_06_22/server

package package .on.json:

    {
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "express-fileupload": "^1.4.0",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.21.0",
    "swagger-autogen": "^2.21.4",
    "swagger-jsdoc": "^6.2.1",
    "swagger-ui-express": "^4.4.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.16"
  }
}

index.js:router.js:

    require('dotenv').config()
const express = require('express')
const sequelize = require('./db')
const models = require('./models/models')
const cors = require('cors')
const fileUpload = require('express-fileupload')
const router = require('./routes/index')
const errorHandler = require('./middleware/ErrorHandlingMiddleware')
const path = require('path')
const swaggerJsDoc = require('swagger-jsdoc')
const swaggerUI = require('swagger-ui-express')
const {Brand} = require("./models/models");


const PORT = process.env.PORT || 5000

const app = express()
app.use(cors())
app.use(express.json())
app.use(express.static(path.resolve(__dirname, 'static')))
app.use(fileUpload({}))
app.use('/api', router)

//Обработка ошибок, последний Middleware
app.use(errorHandler)

const start = async () => {
    try {
        await sequelize.authenticate()
        await sequelize.sync()
        app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
    } catch (e) {
        console.log(e)
    }
}



//Swagger https://swagger.io/specification/#infoObject
const swaggerOptions = {
    swaggerDefinition: {
        info: {
            title: "Documentation Test Store API",
            description: 'API docs',
            termsOfService: 'https://example.com/terms/',
            contact: {
                name: "Admin Api",
                url: "http://localhost:5000",
                email: "[email protected]"
            },
            license: {
                name: "Apache",
                url: "https://www.apache.org/licenses/LICENSE-2.0.html"
            },
            version: "1.0.0",
            servers: {
                    url: "http://localhost:5000",
                    description: "Dev server"
            }
        }
    },
    apis: ["./routes/*.js"]
}

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api/docs', swaggerUI.serve, swaggerUI.setup(swaggerDocs));


start()

router.js:

    const Router = require('express')
const router = new Router()
const brandController = require('../controllers/brandController')
const checkRole = require("../middleware/checkRoleMiddleware")



/**
 * @swagger
 * components:
 *  schemas:
 *      Brand:
 *          type: object
 *          required:
 *              - name
 *          properties:
 *              id:
 *                  type: integer
 *                  format: int64
 *                  description: id brand
 *              name:
 *                  type: string
 *                  description: name brand
 *           example:
 *              id: 1102
 *              name: Vivo
 *              updatedAt: 2022-06-30T17:36:59.924Z
 *              createdAt: 2022-06-30T17:36:59.924Z
 *
 *
 */

/**
 * @swagger
 * /api/brand:
 *  post:
 *      descriptions: Add new brand
 *      responses:
 *          '200':
 *              description: Success
 *              contents:
 *                  application/json:
 *                      schema:
 *                              $ref: '#/components/schemas/Brand'
 *  get:
 *      descriptions: Get All brands
 *      responses:
 *          '200':
 *              description: Success
 *
 */
router.post('/', checkRole('ADMIN'), brandController.create)
router.get('/',brandController.getAll)

module.exports = router

hus pls pl pls!

I'm working in a nodeJS project (express framework)
with swagger-jsdoc and swagger-ui-express

I want to output a schema and an example on the swagger documentation page
I added a schema in the comments:

    /**
 * @swagger
 * components:
 *  schemas:
 *      Brand:
 *          type: object
 *          required:
 *              - name
 *          properties:
 *              id:
 *                  type: integer
... etc.

but it is not displayed:
screen1

my link github: https://github.com/kreotiff-dev/teststore/tree/changes_30_06_22/server

package.json:

    {
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "express-fileupload": "^1.4.0",
    "jsonwebtoken": "^8.5.1",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.21.0",
    "swagger-autogen": "^2.21.4",
    "swagger-jsdoc": "^6.2.1",
    "swagger-ui-express": "^4.4.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.16"
  }
}

index.js:

    require('dotenv').config()
const express = require('express')
const sequelize = require('./db')
const models = require('./models/models')
const cors = require('cors')
const fileUpload = require('express-fileupload')
const router = require('./routes/index')
const errorHandler = require('./middleware/ErrorHandlingMiddleware')
const path = require('path')
const swaggerJsDoc = require('swagger-jsdoc')
const swaggerUI = require('swagger-ui-express')
const {Brand} = require("./models/models");


const PORT = process.env.PORT || 5000

const app = express()
app.use(cors())
app.use(express.json())
app.use(express.static(path.resolve(__dirname, 'static')))
app.use(fileUpload({}))
app.use('/api', router)

//Обработка ошибок, последний Middleware
app.use(errorHandler)

const start = async () => {
    try {
        await sequelize.authenticate()
        await sequelize.sync()
        app.listen(PORT, () => console.log(`Server started on port ${PORT}`))
    } catch (e) {
        console.log(e)
    }
}



//Swagger https://swagger.io/specification/#infoObject
const swaggerOptions = {
    swaggerDefinition: {
        info: {
            title: "Documentation Test Store API",
            description: 'API docs',
            termsOfService: 'https://example.com/terms/',
            contact: {
                name: "Admin Api",
                url: "http://localhost:5000",
                email: "[email protected]"
            },
            license: {
                name: "Apache",
                url: "https://www.apache.org/licenses/LICENSE-2.0.html"
            },
            version: "1.0.0",
            servers: {
                    url: "http://localhost:5000",
                    description: "Dev server"
            }
        }
    },
    apis: ["./routes/*.js"]
}

const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/api/docs', swaggerUI.serve, swaggerUI.setup(swaggerDocs));


start()

router.js:

    const Router = require('express')
const router = new Router()
const brandController = require('../controllers/brandController')
const checkRole = require("../middleware/checkRoleMiddleware")



/**
 * @swagger
 * components:
 *  schemas:
 *      Brand:
 *          type: object
 *          required:
 *              - name
 *          properties:
 *              id:
 *                  type: integer
 *                  format: int64
 *                  description: id brand
 *              name:
 *                  type: string
 *                  description: name brand
 *           example:
 *              id: 1102
 *              name: Vivo
 *              updatedAt: 2022-06-30T17:36:59.924Z
 *              createdAt: 2022-06-30T17:36:59.924Z
 *
 *
 */

/**
 * @swagger
 * /api/brand:
 *  post:
 *      descriptions: Add new brand
 *      responses:
 *          '200':
 *              description: Success
 *              contents:
 *                  application/json:
 *                      schema:
 *                              $ref: '#/components/schemas/Brand'
 *  get:
 *      descriptions: Get All brands
 *      responses:
 *          '200':
 *              description: Success
 *
 */
router.post('/', checkRole('ADMIN'), brandController.create)
router.get('/',brandController.getAll)

module.exports = router

Help pls!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文