openapistack / openapicmd

The CLI for all things OpenAPI and Swagger
https://openapistack.co
MIT License
55 stars 9 forks source link

Add a capability to ignore specific operations from openapi by custom extensions #42

Closed Sureshkumars closed 11 months ago

Sureshkumars commented 11 months ago

Implement exclude-ext functionality to exclude the related operation to the extension and also remove unreferenced resources.

OpenAPI spec with some internal endpoints which should be filtered out for public

info:
  title: My API
  version: 1.0.0
paths:
  /pets:
    get:
      operationId: getPets
      x-internal: true
      responses:
        '200':
          $ref: '#/components/responses/ListPetsRes'
    post:
      operationId: createPet
      requestBody:
        description: Pet object to create
        content:
          application/json: {}
      responses:
        '201':
          $ref: '#/components/responses/PetRes'
  '/pets/{id}':
    get:
      operationId: getPetById
      responses:
        '200':
          $ref: '#/components/responses/PetRes'
      parameters:
      - name: id
        in: path
        required: true
        schema:
          type: integer
      x-internal: true
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
          minimum: 1
        name:
          type: string
          example: Odie
  responses:
    ListPetsRes:
      description: ok
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: '#/components/schemas/Pet'
    PetRes:
      description: ok
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Pet'
  securitySchemes:
    ApiKeyHeaderAuth:
      type: apiKey
      in: header
      name: x-apikey
      description: API key sent as a header
    BasicAuth:
      type: http
      scheme: basic
      description: Basic username/password authentication sent in Authorization header
    BearerAuth:
      type: http
      scheme: bearer
      description: Bearer token sent in Authorization header
    ApiKeyCookieAuth:
      type: apiKey
      in: cookie
      name: apikey
      description: API key sent as a cookie
    ApiKeyQueryAuth:
      type: apiKey
      in: query
      name: apikey
      description: API key sent as a query parameter
security:
  - BasicAuth: []
  - BearerAuth: []
  - ApiKeyHeaderAuth: []
  - ApiKeyCookieAuth: []
  - ApiKeyQueryAuth: []

If we want to strip out all the operations and resources related to the extension x-internal, We could do that now by just

 npx openapicmd read --yaml openapi-with-internal.yml --exclude-ext x-internal > openapi-without-internal.yml

Output

info:
  title: My API
  version: 1.0.0
paths:
  /pets:
    post:
      operationId: createPet
      requestBody:
        description: Pet object to create
        content:
          application/json: {}
      responses:
        '201':
          $ref: '#/components/responses/PetRes'
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
          minimum: 1
        name:
          type: string
          example: Odie
  responses:
    PetRes:
      description: ok
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Pet'
  securitySchemes:
    ApiKeyHeaderAuth:
      type: apiKey
      in: header
      name: x-apikey
      description: API key sent as a header
    BasicAuth:
      type: http
      scheme: basic
      description: Basic username/password authentication sent in Authorization header
    BearerAuth:
      type: http
      scheme: bearer
      description: Bearer token sent in Authorization header
    ApiKeyCookieAuth:
      type: apiKey
      in: cookie
      name: apikey
      description: API key sent as a cookie
    ApiKeyQueryAuth:
      type: apiKey
      in: query
      name: apikey
      description: API key sent as a query parameter
security:
- BasicAuth: []
- BearerAuth: []
- ApiKeyHeaderAuth: []
- ApiKeyCookieAuth: []
- ApiKeyQueryAuth: []