【问题标题】:Is it possible to create environment specific paths in OpenApi 3?是否可以在 OpenApi 3 中创建特定于环境的路径?
【发布时间】:2018-04-19 13:54:43
【问题描述】:

我的目标:

我正在处理应用程序中的一组端点,并且我有一个包含所有端点的 swagger 2.0 文件。他们正在我们的测试环境中工作,但我们还不会在 prod 中提供它们。

我的问题是:

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

我不认为这是因为阅读了文档,但我很想在那里犯错,因为我宁愿只有一个 api.yml 而不是每个环境一个。

谢谢!

【问题讨论】:

  • 这不能单独使用 OpenAPI 完成,您需要 2 个单独的 OpenAPI 定义。您是手动编写 OpenAPI 还是从代码生成它?如果是后者,您的框架可能支持隐藏端点(Swashbuckle、SpringFox、another example)。
  • @Helen 谢谢。它是手动的,我最终为每个环境创建了一个定义

标签: swagger openapi


【解决方案1】:

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

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

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

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

【讨论】:

  • 解析器错误:映射条目的缩进错误。第 21 行 ;-) 修复它会产生更多错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-25
  • 1970-01-01
  • 2019-09-26
  • 2019-05-21
  • 2022-11-06
相关资源
最近更新 更多