是否可以在OpenApi 3中创建特定于环境的路径?

时间:2018-04-19 13:54:44

标签: swagger openapi

我的目标:

我正在处理应用程序中的一组端点,并且我有一个包含所有端点的swagger 2.0文件。他们正在研究我们的测试环境,但我们还没有将它们提供给产品。

我的问题是:

如果我升级到OpenApi 3,是否可以通过servers对象隐藏我不希望在prod中显示的路径?

我并不认为这是因为阅读文档,但我希望在那里出错,因为我更喜欢每个环境只有一个api.yml而不是一个。

谢谢!

1 个答案:

答案 0 :(得分:0)

我做了一些测试,答案是否定的。

  • 您可以选择用户可以运行哪些服务器"尝试一下"测试。这是一个很棒的功能
  • 但是,您无法根据从页面顶部的下拉列表中选择的服务器隐藏端点。这是最初的目标

以下是我在在线编辑器中用来验证的OpenAPI yaml。我在这里使用了在线编辑器:Swagger Editor并使用OpenAPI-Specification github repo提供的petstore.yaml示例作为起点。我删除了除一个端点之外的所有端点以缩短内容。如果您要开始打开api文档,我建议您访问OpenAPISwagger以查找文档和完整示例。

openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
description: Try to not show get when prod server is chosen
license:
    name: MIT
servers:
- url: http://prod      
- url: http://test
- url: http://dev
paths:
/pets:
    get:
    summary: List all pets
    operationId: listPets
    tags:
        - pets
    parameters:
        - name: limit
        in: query
        description: How many items to return at one time (max 100)
        required: false
        schema:
            type: integer
            format: int32
    servers: 
        - url: http://test
        - url: http://dev
    responses:
        200:
        description: An paged array of pets
        headers:
            x-next:
            description: A link to the next page of responses
            schema:
                type: string
        content:
            application/json:    
            schema:
                $ref: "#/components/schemas/Pets"
        default:
        description: unexpected error
        content:
            application/json:
            schema:
                $ref: "#/components/schemas/Error"

components:
schemas:
    Pet:
    required:
        - id
        - name
    properties:
        id:
        type: integer
        format: int64
        name:
        type: string
        tag:
        type: string
    Pets:
    type: array
    items:
        $ref: "#/components/schemas/Pet"
    Error:
    required:
        - code
        - message
    properties:
        code:
        type: integer
        format: int32
        message:
        type: string