Swagger引用externalFile中的定义

问题描述 投票:0回答:1

我有很多招摇文件,使用相同的定义。我想将此定义移动到单独的文件并引用它们。

主要的招摇文件看起来像:

swagger: '2.0'
info:
  version: 1.0.0
basePath: /api
tags:
  - name: MyClient
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /v1/myrequest:
    post:
      tags:
        - PassportCheck
      summary: Проверить паспорт ФЛ
      operationId: passportCheck
      produces:
        - application/json
      parameters:
        - name: Body
          in: body
          required: true
          description: ''
          schema:
            $ref: '#/definitions/MyRequest'
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/MyResponse'
        '400':
          description: Bad request
          schema:
            $ref: '#/definitions/Errors'

definitions: 
  MyRequest:
    type: object
    properties:
      some properties

我正在尝试导入的文件保存到exceptions.yaml(并保存到同一位置),看起来像:

swagger: '2.0'
info:
  version: 1.0.0
  title: Exception API
tags:
  - name: ExceptionAPI
definitions:
  Errors:
    required:
      - errors
    properties:
      errors:
        type: array
        items:
          data declarations go here

我读过$ ref https://swagger.io/docs/specification/using-ref/但是找不到如何导入定义,而不是API

我正在尝试通过以下更改导入它:

    '400':
      description: Bad request
      schema:
        $ref: 'exceptions.yaml#/definitions/Errors'

[ERROR] /C:/Data/MyService/target/generated-sources/src/main/java/myservice/rest/v1/api/V1Api.java:[55,70] cannot find symbol
  symbol:   class ExceptionsYamldefinitionsErrors

或者在不同的变化中使用相对路径

    '400':
      description: Bad request
      schema:
        $ref: '..exceptions.yaml#/definitions/Errors'


[ERROR] Failed to execute goal io.swagger:swagger-codegen-maven-plugin:2.3.1:generate (correqts-adapter) on project individual-client-service: Execution correqts-adapter of goal io.swagger:swagger-codegen-maven-plugin:2.3.1:generate fail
ed: Unable to load RELATIVE ref: ..exceptions.yaml: Could not find ..exceptions.yaml on the classpath ->

或在现有声明下插入$ ref:

definitions:
   $ref: 'exceptions.yaml'  

完全被忽略了

有没有人解决过类似的问题?

swagger swagger-2.0 swagger-codegen
1个回答
0
投票

这适用于我的文件。我确保swagger.yaml和definitions.yaml在同一目录中。

的src /主/招摇/ swagger.yaml

swagger: '2.0'
info:
  title: Stack Overflow
  version: "1.0.0"
# the domain of the service
host: com.stackoverflow
# array of all schemes that your API supports
schemes:
  - https
# will be prefixed to all paths
basePath: /question
consumes:
  - application/json
produces:
  - application/json
paths:
  /hello:
    get:
      operationId: getHelloWorlds
      produces:
        - application/json
      responses:
        200:
          schema:
            type: array
            items:
              $ref: 'definitions.yaml#/definitions/HelloWorldObject'

的src /主/招摇/ definitions.yaml

swagger: '2.0'
info:
  title: Stack Overflow API Models
  version: "1.0.0"
definitions:
  HelloWorldObject:
    type: object
    properties:
      hello: string
      world: string

pom.xml(snippet:project / build / plugins)

<plugin>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.2</version>

  <executions>
    <execution>
      <id>generate-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>generate</goal>
      </goals>
      <configuration>
        <inputSpec>${basedir}/src/main/swagger/swagger.yaml</inputSpec>
        <configurationFile>${basedir}/src/main/swagger/config.json</configurationFile>
        <language>jaxrs-di</language>
        <apiPackage>${swagger.api.package}</apiPackage>
        <modelPackage>${swagger.model.package}</modelPackage>
        <generateSupportingFiles>false</generateSupportingFiles>
        <modelNameSuffix>Model</modelNameSuffix>
      </configuration>
    </execution>
  </executions>
</plugin>
© www.soinside.com 2019 - 2024. All rights reserved.