Coder Social home page Coder Social logo

Add OpenAPI example factories about sanic-ext HOT 3 OPEN

sanic-org avatar sanic-org commented on July 21, 2024
Add OpenAPI example factories

from sanic-ext.

Comments (3)

ahopkins avatar ahopkins commented on July 21, 2024

Makes absolute sense, and I would happily welcome a PR. The code base was largely copied from the earlier sanic-openapi package, which had very limited type hinting support. Because of that, there are a lot of fields that are implicit and not easily discovered. This is something that needs to be resolved in the long run. In the short run, I think that it exposed a problem that example is not one of them as you indicated.

from sanic-ext.

sorenrife avatar sorenrife commented on July 21, 2024

Cool! I was thinking that since auto-generated examples could lead to unrealistic examples, it could be useful to have a FactoryBoy-ready type approach, where example accepts an instance of RequestBody.body, Response.content etc

So you could have

@openapi.definition(
    summary="Create a new MusicID",
    description="Create a new **MusicID** via a deserialized MusicID",
    body=RequestBody(serializers.MusicIDRequest, example=MusicIDRequestFactory.build()),
    response=[
        oa.Response(Error, status=429, description="too many requests"),
        oa.Response(Error, status=400, description="bad request"),
        oa.Response(Error, status=401, description="user not authenticated"),
        oa.Response(serializers.MusicIDResponse, example=serializers.MusicIDResponseFactory.build()),
    ],
)

And the example fields would be realistic since we'd permit the usage of Faker, FuzzyAttributes...
So is up to the User to match the criteria of the field

class MusicIDRequestFactory(factory.Factory):
    """
    MusicIDRequest factory
    """

    name: str = factory.Sequence(lambda n: "Section configuration %s" % n)
    identifier: str = faker.Faker('uuid')
    creation_title: str = factory.Sequence(lambda n: "Create your Section %s!" % n)
    source: str = fuzzy.FuzzyChoice(SectionConfig.Source, getter=lambda x: x[0])
    optional: bool = False
    max_items: int = fuzzy.FuzzyInteger(1, 10)

    class Meta:
        model = MusicIDRequestFactory

I'd love to hear your thoughts about it

from sanic-ext.

ahopkins avatar ahopkins commented on July 21, 2024

Ooo! That's a nice touch. I'm on board with this. Let me know if you want to spec it out with me, or if you want to just throw together a proposal first.

from sanic-ext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.