Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1K
Visualizações
How to use Marshmallow Schema in Flask-restx Swagger UI Automatically

I'm trying to create a restful web services with flask-restx and marshmallow.

I'm using marshmallow for both request and response validations.

Since flask-restx api docs does not support marshmallow schemas in swagger ui, i want to add it using doc decorator.

Controller Code:

@ns.route('/')
class Test(Resource):
    @ns.doc(params={'test': 'test'})
    def get(self):
        _input_schema = MySchema()
        errors = _input_schema.validate(request.args)
        if errors:
            return Response(str(errors), status=400)
        other_things()

Schema Code:

class MySchema(Schema):
    title = fields.Str()
    id = fields.Integer()
    slug = fields.Str()

I'm trying to automatically add parameters from schema to api docs like that

@ns.doc(params=MySchema.ReturnAFieldDict())

And it will give something like that

@ns.doc(params={"title":"A string", "id": "Int value with min and max", "slug":"A str"})

Is there any way to do that?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

One way I found to get documentation is to use the @api.expect(schema) decorator.

over 4 years ago · Santiago Trujillo Relatório

0

You can use api.marshal_with to document responses

from flask_restx.fields import Integer, String
test_get = api.model(
    "TestGet",
    {
        "name": String(required=True),
        "age": Integer(required=True),
    },
)
@api.marshal_with(schema.test_get, code=200)
def get(self):
    return {"name": "test", "age": 123}

marshal_with maps the returned dictionary or object and maps the field as per the schema. Here, in this case the returned dictionary will map on test_get schema where name will be "test" and age will be 123.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda