MarketSquare / MarketSquare/robotframework-openapitools

improvement suggestion: class methods for data mapping

Open
#10 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

documentation
Dominant language
Python
Stars
28
Forks
7
PR merge metrics
No merged PRs in 30d

Description

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.

```python
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:

```python
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:

```python
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:

```python
class DtoOtherChild(DtoCommon):
IGNORED_PROPERTIES = ["limit", "age"]
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the documentation covering data mapping, DTOs, and OpenAPI parameter or property relations. Explain the proposed parent and child class pattern, including ignored properties and parameters, using the examples in the issue; done means the inheritance approach is clearly documented without requiring a code change.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.