MarketSquare / robotframework-openapitools

Apache License 2.0
6 stars 1 forks source link

improvement suggestion: class methods for data mapping #10

Open Leemur89 opened 6 months ago

Leemur89 commented 6 months ago

Hello,

When most endpoints share a common structure (like headers or other parameters), it could be useful to have a parent Dto class from which inheriting all Dto classes. No code change is require, but the following snippets could complement the documentation of the data mapping.

For instance in my case I have a common parameter customer-name that is present in the OAS in most endpoints.

class DtoCommon(Dto):
    CUSTOMER = os.getenv("CUSTOMER")

    @classmethod
    def get_parameter_relations(cls) -> List:
        customer = PropertyValueConstraint(
            property_name="customer-name", values=[cls. CUSTOMER]
        )
        return ([
                customer,
            ]
            + cls.parameter_relations()
        )

    @classmethod
    def parameter_relations(cls) -> List:
        return []

    @classmethod
    def get_relations(cls) -> List:
        return cls.relations()

    @classmethod
    def relations(cls) -> List:
        return []

And then create the "child" classes simply as follow:

class DtoChild(DtoCommon):
    @classmethod
    def parameter_relations(cls) -> List:
        return [IdDependency(
          property_name="wagegroup_id",
          get_path="/wagegroups",
          error_code=451,
        )]

And I can go even further by adding class attributes that would list the ignored properties:

class DtoCommon(Dto):
    CUSTOMER = os.getenv("CUSTOMER")
    IGNORED_PROPERTIES: List[str] = []
    IGNORED_PARAMETERS: List[str] = []

    @classmethod
    def get_parameter_relations(cls) -> List:
        customer = PropertyValueConstraint(
            property_name="customer-name", values=[cls. CUSTOMER]
        )
        ignored_parameters = list(
            map(
                lambda value: PropertyValueConstraint(
                    property_name=value, values=[IGNORE]
                ),
                cls.IGNORED_PARAMETERS,
            )
        )
        return (
            ignored_parameters
            + [
                customer,
            ]
            + cls.parameter_relations()
        )

    @classmethod
    def parameter_relations(cls) -> List:
        return []

    @classmethod
    def get_relations(cls) -> List:
        ignored_properties = list(
            map(
                lambda value: PropertyValueConstraint(
                    property_name=value, values=[IGNORE]
                ),
                cls.IGNORED_PROPERTIES,
            )
        )
        return ignored_properties + cls.relations()

    @classmethod
    def relations(cls) -> List:
        return []

And simply add the list of ignored properties in the child class:

class DtoOtherChild(DtoCommon):
  IGNORED_PROPERTIES = ["limit", "age"]
robinmackaij commented 6 months ago

Having some form of "advanced mapping guide" would be nice to have. In a client project, we're doing similar things, but I can't share that as public examples.

The design in indeed an ABC implementation to allow advanced logic in Python to be flexible in handling different scenarios.