最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

“不支持的 OpenAPISwagger 版本 = 未定义”。无法对节点使用 express

网站源码admin43浏览0评论

“不支持的 OpenAPI / Swagger 版本 = 未定义”。无法对节点使用 express

“不支持的 OpenAPI / Swagger 版本 = 未定义”。无法对节点使用 express

我想用 express-openapi-validator 验证我的 api 请求,但每次我收到这个错误“Unsupported OpenAPI / Swagger version=undefined”。

不知道为什么会这样,只是为了我,我已经尝试过 github 上的其他人的代码也与他们的代码相同。

我卡住了。

我的 apiSpec.yaml 文件

openapi: 3.0.0
info:
  version: 1.0.0
  title: API validation using OpenAPI
paths:
  /contact:
    get:
      tags:
        - Contact
      summary: Get all the contacts
      description: Get all the contacts
      responses:
        "200":
          description: Contacts fetched successfully
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Contact"
        "400":
          description: Error in fetching contacts
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
    post:
      tags:
        - Contact
      summary: Save a new contact
      description: Save a new contact
      requestBody:
        $ref: "#/components/requestBodies/Contact"
      responses:
        "200":
          description: Contact saved successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
        "400":
          description: Error in saving contact
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
  "/contact/{id}":
    get:
      tags:
        - Contact
      summary: Get a contact
      description: Get a contact with the id specified in parameter
      parameters:
        - in: path
          name: id
          description: Contact id that needs to be fetched
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Contact fetched successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Contact"
        "400":
          description: Error in fetching contact
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
    put:
      tags:
        - Contact
      summary: Update a contact
      description: Update a contact
      parameters:
        - in: path
          name: id
          description: Contact id that needs to be updated
          required: true
          schema:
            type: string
      requestBody:
        $ref: "#/components/requestBodies/Contact"
      responses:
        "200":
          description: Contact updated successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
        "400":
          description: Error in updating contact
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
    delete:
      tags:
        - Contact
      summary: Delete a contact
      description: Delete a contact with the id specified in parameter
      parameters:
        - in: path
          name: id
          description: Contact id that needs to be deleted
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Contact deleted successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
        "400":
          description: Error in deleting contact
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
  /product:
    get:
      tags:
        - Product
      summary: Get all the products
      description: Get all the products
      responses:
        "200":
          description: Products fetched successfully
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/Product"
        "400":
          description: Error in fetching products
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
    post:
      tags:
        - Product
      summary: Save a new product
      description: Save a new product
      requestBody:
        $ref: "#/components/requestBodies/Product"
      responses:
        "200":
          description: Product saved successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
        "400":
          description: Error in saving product
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
  "/product/{id}":
    get:
      tags:
        - Product
      summary: Get a product
      description: Get a product with the id specified in parameter
      parameters:
        - in: path
          name: id
          description: Product id that needs to be fetched
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Product fetched successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Product"
        "400":
          description: Error in fetching product
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
    put:
      tags:
        - Product
      summary: Update a product
      description: Update a product
      parameters:
        - in: path
          name: id
          description: Product id that needs to be updated
          required: true
          schema:
            type: string
      requestBody:
        $ref: "#/components/requestBodies/Product"
      responses:
        "200":
          description: Product updated successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
        "400":
          description: Error in updating product
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
    delete:
      tags:
        - Product
      summary: Delete a product
      description: Delete a product with the id specified in parameter
      parameters:
        - in: path
          name: id
          description: Product id that needs to be deleted
          required: true
          schema:
            type: string
      responses:
        "200":
          description: Product deleted successfully
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
        "400":
          description: Error in deleting product
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CommonResponse"
servers:
  - url: http://localhost:3000/api
  - url: https://localhost:3000/api
components:
  requestBodies:
    Contact:
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Contact"
      description: Contact object
      required: true
    Product:
      content:
        application/json:
          schema:
            $ref: "#/components/schemas/Product"
      description: Product object
      required: true
  schemas:
    CommonResponse:
      type: object
      properties:
        message:
          type: string
    Contact:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        mobile:
          type: string
        email:
          type: string
    Product:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
        price:
          type: number

我的 App.js

"use strict";

const express = require('express');
const { OpenApiValidator } = require("express-openapi-validate");
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();

// ** Setting Up Port
const port = 3000;

// ** Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true, }));

// ** Adding Cors
app.use(cors());

// ** Open API Validator
new OpenApiValidator({
    "apiSpecPath": "./apiSpec.yaml"
}).install(app)

app.get("/", (req, res) => {
    res.send(`App Up & Running!`);
});

const contactList = [
    {
        id: '1',
        name: 'Johnny Depp',
        email: '[email protected]',
        mobile: '1234567'
    },
    {
        id: '2',
        name: 'Angelina Jolie',
        email: '[email protected]',
        mobile: '1234567'
    }
];

app.get('/api/contact', (req, res) => {
    res.status(200).json(contactList);
});

app.post("/api/contact", (req, res) => {
    return res.json({ message: "Added successfully" });
});

app.get("/api/contact/:id", (req, res) => {

});

app.put("/api/contact/:id", (req, res) => {

});

app.delete("/api/contact/:id", (req, res) => {

});


app.listen(port, () => {
    console.log(`App listening at http://localhost:${port}`);
});

还尝试了多种其他方式,但仅以此错误结束

“不支持的 OpenAPI / Swagger 版本=未定义”

回答如下:
发布评论

评论列表(0)

  1. 暂无评论