Skip to content

Sparse fieldsets for invalid type results in 500 #64

@ddobson

Description

@ddobson

Description
When attempting to use sparse fieldsets for an invalid type, a 500 is raised. Is this the correct functionality? I would think this is more of a 400 level error. To demonstrate, if one were to make a request for GET /cars?fields[dealership]=name using the code below, they would get the documented 500 response. I would be happy to open a PR to change this functionality if there is agreement that there is a better way to handle these errors (i.e. raise 4XX error).

Problematic Code

raise Exception("Couldn't find schema for type: {}".format(resource_type))

Example

# Example
class Car(Base):
    id = Column(Int, primary_key=True)
    make = Column(Str)
    model = Column(Str)

class CarSchema
    class Meta:
        type_ = "car"

    make = fields.Str()
    model = fields.Str()

class CarListResource(ResourceList):
    methods = ["GET"]
    schema = CarSchema
    data_layer = {"session": db.session, "model": Car}

app = Flask(__name__)
api = Api(app)
api.route(
    CarListResource, "car_resource", "/cars",
)

Example Response

// GET /cars?fields[dealership]=name`
// status code: 500

{
  "errors": [
    {
      "status": "500",
      "title": "Unknown error",
      "detail": "Couldn't find schema for type: dealership"
    }
  ],
  "jsonapi": {
    "version": "1.0"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions