1

My Goal:

I am working on a set of endpoints in an application and I have a swagger 2.0 file with all of the endpoints. They are working on our test environment, but we will not yet make them available in prod.

My question is:

If I upgrade to OpenApi 3, is it possible to hide the paths I don't want to be visible in prod via the servers object?

I didn't think it was from reading the docs, but I would love to be wrong there because I'd prefer to have just one api.yml instead of one for each environment.

Thank you!

BenD
  • 118
  • 1
  • 10
  • This cannot be done using an OpenAPI alone, you'll need 2 separate OpenAPI definitions. Do you write OpenAPI manually or generate it from code? If the latter, your framework might support hiding endpoints ([Swashbuckle](https://stackoverflow.com/q/29701573/113116), [SpringFox](https://stackoverflow.com/q/43935041/113116), [another example](https://stackoverflow.com/q/39788099/113116)). – Helen Apr 19 '18 at 16:04
  • @Helen Thanks. It was manually, I ended up creating a definition per environment – BenD Apr 19 '18 at 20:59

1 Answers1

0

I did some testing and the answer is no.

  • You CAN chose which servers users can run a "try it out" test. Which is a great feature
  • But, you can NOT hide an endpoint based on which server is chosen from the dropdown at the top of the page. Which was the original goal

Below is the OpenAPI yaml I used in an online editor to verify. I used the online editor here: Swagger Editor and used the petstore.yaml example provided at the OpenAPI-Specification github repo as a starting point. I removed all but one endpoint to shorten things. If you are starting an open api document I would recommend visiting OpenAPI or Swagger to find documentation and a full example.

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
BenD
  • 118
  • 1
  • 10