首页 > 解决方案 > 将 Swagger 规范拆分为较小的文件时出错

问题描述

我的文件夹结构如下

/spec
    /definitions
        index.yaml
        User.yaml
    /paths
        ...
    spec.yaml

里面的内容index.yaml

User:
  $ref: ./User.yaml

里面的内容User.yaml

type: "object"
properties:
  id:
    type: "integer"
    format: "int64"
  first_name:
    type: "string"
  last_name:
    type: "string"
  email:
    type: "string"
  password:
    type: "string"
  phone:
    type: "string"
xml:
  name: "User"
example:
  first_name: "firstName"
  last_name: "lastName"
  password: "password"
  phone: "phone"
  id: 0
  email: "email"

在里面spec.yaml,我定义如下

---
swagger: "2.0"
info:
  description: "This is a sample."
  version: "1.0.0"
  title: "Smart Community API Documents"
  termsOfService: "http://swagger.io/terms/"
  contact:
    email: "email@gmail.com"
  license:
    name: "Apache 2.0"
    url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "localhost:4000"
basePath: "/"
tags:
- name: "user"
  description: "Operations about user"
  externalDocs:
    description: "Find out more about our store"
    url: "http://swagger.io"
schemes:
- "http"
paths:
  /user:
    post:
      tags:
      - "user"
      summary: "Create user"
      description: "This can only be done by the logged in user."
      operationId: "createUser"
      produces:
      - "application/xml"
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Created user object"
        required: true
        schema:
          $ref: "#/definitions/User"
      responses:
        default:
          description: "successful operation"
      x-swagger-router-controller: "User"
securityDefinitions:
  petstore_auth:
    type: "oauth2"
    authorizationUrl: "http://petstore.swagger.io/oauth/dialog"
    flow: "implicit"
    scopes:
      write:pets: "modify pets in your account"
      read:pets: "read your pets"
  api_key:
    type: "apiKey"
    name: "api_key"
    in: "header"
definitions:
  User:
    $ref: ./definitions/User.yaml
externalDocs:
  description: "Find out more about Swagger"
  url: "http://swagger.io"

然后,我得到了错误,

API 错误:

#/definitions/User/$ref:无法解析引用:./definitions/User.yaml

有人可以帮我吗?

标签: yamlswagger-2.0

解决方案


推荐阅读