swagger-marshmallow-codegen icon indicating copy to clipboard operation
swagger-marshmallow-codegen copied to clipboard

rewriting the handle way of paths part

Open podhmo opened this issue 5 years ago • 1 comments

if the openapi.json is existed, such as below.

https://petstore.swagger.io/v2/swagger.json

The current implementation generates the code is here.

$ swagger-marshmallow-codegen --full petstore.json
# this is auto-generated by swagger-marshmallow-codegen
from __future__ import annotations
from marshmallow import (
    Schema,
    fields,
    INCLUDE,
)
from marshmallow.validate import OneOf
from swagger_marshmallow_codegen.validate import Range
from swagger_marshmallow_codegen.schema import (
    AdditionalPropertiesSchema,
    PrimitiveValueSchema,
)


class ApiResponse(Schema):
    code = fields.Integer()
    type = fields.String()
    message = fields.String()

    class Meta:
        unknown = INCLUDE



class Category(Schema):
    id = fields.Integer()
    name = fields.String()

    class Meta:
        unknown = INCLUDE



class Pet(Schema):
    id = fields.Integer()
    category = fields.Nested(lambda: Category())
    name = fields.String(required=True)
    photoUrls = fields.List(fields.String(), required=True)
    tags = fields.List(fields.Nested(lambda: Tag()))
    status = fields.String(description='pet status in the store', validate=[OneOf(choices=['available', 'pending', 'sold'], labels=[])])

    class Meta:
        unknown = INCLUDE



class Tag(Schema):
    id = fields.Integer()
    name = fields.String()

    class Meta:
        unknown = INCLUDE



class Order(Schema):
    id = fields.Integer()
    petId = fields.Integer()
    quantity = fields.Integer()
    shipDate = fields.AwareDateTime()
    status = fields.String(description='Order Status', validate=[OneOf(choices=['placed', 'approved', 'delivered'], labels=[])])
    complete = fields.Boolean()

    class Meta:
        unknown = INCLUDE



class User(Schema):
    id = fields.Integer()
    username = fields.String()
    firstName = fields.String()
    lastName = fields.String()
    email = fields.String()
    password = fields.String()
    phone = fields.String()
    userStatus = fields.Integer(description='User Status')

    class Meta:
        unknown = INCLUDE



class PetPetIdUploadImageInput:
    class Post:
        """
        uploads an image
        """

        class FormData(Schema):
            additionalMetadata = fields.String(description='Additional data to pass to server')
            file = fields.Field(description='file to upload')

            class Meta:
                unknown = INCLUDE


        class Path(Schema):
            petId = fields.Integer(required=True, description='ID of pet to update')

            class Meta:
                unknown = INCLUDE





class PetInput:
    class Post:
        """
        Add a new pet to the store
        """

        class Body(Pet):

            class Meta:
                unknown = INCLUDE



    class Put:
        """
        Update an existing pet
        """

        class Body(Pet):

            class Meta:
                unknown = INCLUDE





class PetFindByStatusInput:
    class Get:
        """
        Finds Pets by status
        """

        class Query(Schema):
            status = fields.List(fields.String(missing=lambda: 'available', validate=[OneOf(choices=['available', 'pending', 'sold'], labels=[])]), required=True, description='Status values that need to be considered for filter')

            class Meta:
                unknown = INCLUDE





class PetFindByTagsInput:
    class Get:
        """
        Finds Pets by tags
        """

        class Query(Schema):
            tags = fields.List(fields.String(), required=True, description='Tags to filter by')

            class Meta:
                unknown = INCLUDE





class PetPetIdInput:
    class Get:
        """
        Find pet by ID
        """

        class Path(Schema):
            petId = fields.Integer(required=True, description='ID of pet to return')

            class Meta:
                unknown = INCLUDE



    class Post:
        """
        Updates a pet in the store with form data
        """

        class FormData(Schema):
            name = fields.String(description='Updated name of the pet')
            status = fields.String(description='Updated status of the pet')

            class Meta:
                unknown = INCLUDE


        class Path(Schema):
            petId = fields.Integer(required=True, description='ID of pet that needs to be updated')

            class Meta:
                unknown = INCLUDE



    class Delete:
        """
        Deletes a pet
        """

        class Header(Schema):
            api_key = fields.String()

            class Meta:
                unknown = INCLUDE


        class Path(Schema):
            petId = fields.Integer(required=True, description='Pet id to delete')

            class Meta:
                unknown = INCLUDE





class StoreOrderInput:
    class Post:
        """
        Place an order for a pet
        """

        class Body(Order):

            class Meta:
                unknown = INCLUDE





class StoreOrderOrderIdInput:
    class Get:
        """
        Find purchase order by ID
        """

        class Path(Schema):
            orderId = fields.Integer(required=True, description='ID of pet that needs to be fetched', validate=[Range(min=1, max=10, min_inclusive=True, max_inclusive=True)])

            class Meta:
                unknown = INCLUDE



    class Delete:
        """
        Delete purchase order by ID
        """

        class Path(Schema):
            orderId = fields.Integer(required=True, description='ID of the order that needs to be deleted', validate=[Range(min=1, max=None, min_inclusive=True, max_inclusive=True)])

            class Meta:
                unknown = INCLUDE





class StoreInventoryInput:
    class Get:
        """
        Returns pet inventories by status
        """

        pass



class UserCreateWithArrayInput:
    class Post:
        """
        Creates list of users with given input array
        """

        class Body(User):
            def __init__(self, *args, **kwargs):
                kwargs['many'] = True
                super().__init__(*args, **kwargs)


            class Meta:
                unknown = INCLUDE





class UserCreateWithListInput:
    class Post:
        """
        Creates list of users with given input array
        """

        class Body(User):
            def __init__(self, *args, **kwargs):
                kwargs['many'] = True
                super().__init__(*args, **kwargs)


            class Meta:
                unknown = INCLUDE





class UserUsernameInput:
    class Get:
        """
        Get user by user name
        """

        class Path(Schema):
            username = fields.String(required=True, description='The name that needs to be fetched. Use user1 for testing. ')

            class Meta:
                unknown = INCLUDE



    class Put:
        """
        Updated user
        """

        class Body(User):

            class Meta:
                unknown = INCLUDE


        class Path(Schema):
            username = fields.String(required=True, description='name that need to be updated')

            class Meta:
                unknown = INCLUDE



    class Delete:
        """
        Delete user
        """

        class Path(Schema):
            username = fields.String(required=True, description='The name that needs to be deleted')

            class Meta:
                unknown = INCLUDE





class UserLoginInput:
    class Get:
        """
        Logs user into the system
        """

        class Query(Schema):
            username = fields.String(required=True, description='The user name for login')
            password = fields.String(required=True, description='The password for login in clear text')

            class Meta:
                unknown = INCLUDE





class UserLogoutInput:
    class Get:
        """
        Logs out current logged in user session
        """

        pass



class UserInput:
    class Post:
        """
        Create user
        """

        class Body(User):

            class Meta:
                unknown = INCLUDE

podhmo avatar Nov 10 '20 20:11 podhmo

bad

  • path information is lost
  • nested classes are Unfamiliar
  • cannot use directly

good

  • OperationId is not needed

podhmo avatar Nov 10 '20 20:11 podhmo