marshmallow-code / marshmallow

A lightweight library for converting complex objects to and from simple Python datatypes.
https://marshmallow.readthedocs.io/
MIT License
7.06k stars 629 forks source link

Send a post request containing nested data, .List(fields.Nested(Schema)), Flask restapi - marshmallow schema #2302

Open ghost opened 3 months ago

ghost commented 3 months ago

I have a problem when requesting post with list (nested (schema) so that the input type is not accepted { "code": 422, "errors": { "form": { "addresses": { "0": { "_schema": [ "Invalid input type." ] } } } }, "status": "Unprocessable Entity" }

I am sending the data as form data because I am sending files in the request, I do not want to send the request as json

The request I sent is:

addresses:[{"city":"aa","street_name":"a5"}]

and addresses:{"city":"aa","street_name":"a5"}

It works without schema but I want to use schema to check the validity of the data @blp.arguments(Schema, location="form")

I did not find a solution to this problem, can anyone help me

class Address(db.Model): id = db.Column(db.Integer, primary_key=True) city = db.Column(db.String(100), nullable=False) street_name = db.Column(db.String(150), nullable=False) store_id = db.Column(db.Integer, db.ForeignKey('store.id'), nullable=False) store= db.relationship('Store', backref='addresses ')

class Store(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) logo = db.Column(db.String(200), nullable=True) addresses = db.relationship('Address', backref='store', lazy=True)

class AddressSchema(Schema): id = fields.Int(dump_only=True) city = fields.Str(required=True) street_name = fields.Str(required=True) store_id = fields.Int(required=True)

class StoreSchema(Schema): id = fields.Int(dump_only=True) name = fields.Str(required=True) logo = fields.Str() addresses = fields.List(fields.Nested(AddressSchema))

deckar01 commented 3 months ago

This appears to be a duplicate of https://github.com/marshmallow-code/flask-smorest/issues/253.

The form location implies that the data is a url encoded, not JSON.

Multipart requests with mixed types (file, form, etc.) are not supported. They can be achieved but the documentation is not correctly generated. arguments decorator can be called multiple times on the same view function but it should not be called with more that one request body location. This limitation is discussed in #46.

https://flask-smorest.readthedocs.io/en/latest/arguments.html#content-type

ghost commented 3 months ago

in send post request form data: name : ss logo :image addresses:[{"city":"aa","street_name":"a5"}]

My problem is only in sending nested data and not in uploading files. I get this result in postman: { "code": 422, "errors": { "form": { "addresses": { "0": { "_schema": [ "Invalid input type." ] } } } }, "status": "Unprocessable Entity" }

ghost commented 3 months ago

This appears to be a duplicate of marshmallow-code/flask-smorest#253.

The form location implies that the data is a url encoded, not JSON.

Multipart requests with mixed types (file, form, etc.) are not supported. They can be achieved but the documentation is not correctly generated. arguments decorator can be called multiple times on the same view function but it should not be called with more that one request body location. This limitation is discussed in #46.

https://flask-smorest.readthedocs.io/en/latest/arguments.html#content-type

in send post request form data: name : ss logo :image addresses:[{"city":"aa","street_name":"a5"}]

My problem is only in sending nested data and not in uploading files. I get this result in postman: { "code": 422, "errors": { "form": { "addresses": { "0": { "_schema": [ "Invalid input type." ] } } } }, "status": "Unprocessable Entity" }

deckar01 commented 3 months ago

Have you tried the custom parser work around in that first issue?

https://github.com/marshmallow-code/flask-smorest/issues/253#issuecomment-860540752