Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/expose only supported methods #35

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

AndreasBBS
Copy link

Expose only implemented methods

Make this Plugin list only implemented methods instead of all possible methods

Example:

For the path defined by the class:

import falcon
from schemas.Pet import PetSchema

from app import app, spec

from random import randint, choices
from string import ascii_uppercase, digits

class RandomPetResource:
    async def on_get(self, req, resp):
        """A cute furry animal endpoint.
        ---
        description: Get a random pet
        responses:
            200:
                description: A pet to be returned
                schema: PetSchema
        """
        pet = {
            "id": randint(1, 100),
            "name": ''.join(
                choices(ascii_uppercase + digits, k=10)
            )
        }
        resp.media = PetSchema().dump(pet)
        resp.status = falcon.HTTP_200

# Register Pet Resource
pets = RandomPetResource()
app.add_route('/pet', pets)
spec.path(resource=pets)

Before the fix, the resulting swagger.json looks like:

{
  "paths": {
    "/pet": {
      "get": {
        "description": "Get a random pet",
        "responses": {
          "200": {
            "description": "A pet to be returned",
            "schema": {
              "$ref": "#/components/schemas/Pet"
            }
          }
        }
      },
      "options": {},
      "delete": {},
      "head": {},
      "patch": {},
      "post": {},
      "put": {},
      "trace": {}
    }
  },
  "info": {
    "title": "TenScores API",
    "version": "0.0.1"
  },
  "openapi": "3.0.2",
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          }
        },
        "required": [
          "name"
        ]
      }
    }
  }
}

After the fix, the resulting swagger.json looks like:

{
  "paths": {
    "/pet": {
      "get": {
        "description": "Get a random pet",
        "responses": {
          "200": {
            "description": "A pet to be returned",
            "schema": {
              "$ref": "#/components/schemas/Pet"
            }
          }
        }
      }
    }
  },
  "info": {
    "title": "TenScores API",
    "version": "0.0.1"
  },
  "openapi": "3.0.2",
  "components": {
    "schemas": {
      "Pet": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "id": {
            "type": "integer"
          }
        },
        "required": [
          "name"
        ]
      }
    }
  }
}

It's also more efficient because, instead of going through all the routes and creating a mapping for each resource every time spec.path gets called, it filters for the route with the passed resource.

Feel free to try my changes by using pip install git+https://github.com/AndreasBBS/falcon-apispec.git@fix/expose_only_supported_methods#egg=falcon-apispec

NOTE: I'm also including the changes in this PR #32 that's taking ages to get merged cause I couldn't be bothered to change falcon to a version prior to 3.0

Le Van Vuong and others added 9 commits June 13, 2021 12:46
- Get route for specific resource instead of creating a mapping for
all resources using filter
- Get uri from route
- Get methods from resource with falcon.routing.map_http_methods
instead of getting methods from route.method_map.

NOTE: The main difference is that route.method_map returns all
methods ["GET", "POST", "PUT", ...] regardless if the resource is
implementing that method. Using falcon.routing.map_http_methods only
implemented methods are listed.
This allows for a much cleaner API Spec that doesn't list unsupported
methods.
@AndreasBBS AndreasBBS force-pushed the fix/expose_only_supported_methods branch from de0b632 to da172c9 Compare December 8, 2022 02:51
@AndreasBBS
Copy link
Author

@alysivji @aslantar @Javlopez Forgot to put you as reviewrs and can't do it now. Please set yourselves. I'd love to hear back if you think this is useful too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants