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

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

Open
ghost opened this issue Sep 2, 2024 · 4 comments

Comments

@ghost
Copy link

ghost commented Sep 2, 2024

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
Copy link
Member

deckar01 commented Sep 2, 2024

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

@ghost
Copy link
Author

ghost commented Sep 3, 2024

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
Copy link
Author

ghost commented Sep 3, 2024

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
Copy link
Member

deckar01 commented Sep 3, 2024

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

marshmallow-code/flask-smorest#253 (comment)

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

No branches or pull requests

1 participant