Skip to content

Consistency between fallback of RequestValidation Middleware and Lifecycle #2117

@AntoineDuComptoirDesPharmacies

Description

Problem

We notice that the fallback used in RequestValidation and Lifecycle are not aligned.

In RequestValidation:
The fallback is the first mime type defined in the openapi file if one is defined and if None, then it is "application/octet-stream".

In Lifecycle:
The fallback in case of missing Content-Type is "application/octet-stream".

This is resulting in some bugs when mime-type is not defined when request body is a JSON.
Indeed, in the following code, self.content_type equals "application/octet-stream" resulting in object "bytes" being returned :

  async def get_body(self):                                                                                                                                                                                                                                                                                         
      if is_json_mimetype(self.content_type):                                                                                                                                                                                                                                                                       
          return await self.json()                                                                                                                                                                                                                                                                                  
      ...                                                                                                                                                                                                                                                                                                           
      else:
          return await self.body() or None        
WSGI_0s - 0 0 - ERROR - api.provide.impl.magic_cart.controllers.manage_express_order_controller - 'bytes' object has no attribute 'buyer_id'
Traceback (most recent call last):
  File "/home/brultea/IdeaProjects/lcdp-magic-cart/api/provide/impl/magic_cart/controllers/manage_express_order_controller.py", line 26, in create_express_order
    buyer_id = express_order_creation_parameters.buyer_id
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'bytes' object has no attribute 'buyer_id'
AnyIO worker threads - 0 0 - ERROR - connexion.middleware.exceptions - HTTPException(status_code=500, detail="'bytes' object has no attribute 'buyer_id'")
AnyIO worker threads - 0 0 - ERROR - connexion.middleware.exceptions - HTTPException(status_code=500, detail="'bytes' object has no attribute 'buyer_id'")
MainThreads - 0 0 - INFO - uvicorn.access - 127.0.0.1:50613 - "POST /api/v1/express-orders HTTP/1.1" 500

This issue may be linked to #1269.

Proposal

In RequestValidation, if we fallback on a mime type located in the OpenApi file, then we should change the scope header "Content-Type" to match the fallback.

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