Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping types in OpenAPI schema #476

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

tyzhnenko
Copy link
Contributor

@tyzhnenko tyzhnenko commented Jan 27, 2024

It is useful to return some grouped data

@dataclass
class User:
    id: str
    name: str

Example for Dict[str, list[User]]

{
  "type": "object",
  "additionalProperties": {
    "type": "array",
    "items": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" }
      },
      "required": ["id", "name"]
    }
  }
}

It is useful to return some grouped data
```python
@DataClass
class User:
    id: str
    name: str
```

Example for `Dict[str, list[User]]`

```json
{
  "type": "object",
  "patternProperties": {
    "^[a-z0-9!\"#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]+$": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "name": { "type": "string" }
        },
        "required": ["id", "name"]
      }
    }
  }
}

```
@tyzhnenko
Copy link
Contributor Author

I've pinned black to 23.12.1 because in the 24 release it wants to make a lot of changes. I guess it should be a separate patch.

@codecov-commenter
Copy link

codecov-commenter commented Jan 27, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (bec8e14) 96.37% compared to head (7d6659b) 96.35%.

Files Patch % Lines
blacksheep/server/openapi/v3.py 84.21% 3 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #476      +/-   ##
==========================================
- Coverage   96.37%   96.35%   -0.03%     
==========================================
  Files          69       69              
  Lines        6487     6506      +19     
==========================================
+ Hits         6252     6269      +17     
- Misses        235      237       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@RobertoPrevato
Copy link
Member

I don't understand what is the meaning of this fragment:

                    description: Success response
                    content:
                        application/json:
                            schema:
                                type: object
                                properties:
                                    ^[a-z0-9!\"#$%&'()*+,.\/:;<=>?@\[\] ^_`{|}~-]+$:
                                        type: object
                                        properties:
                                            ^[0-9]+$:
                                                $ref: '#/components/schemas/Cat'
                                        nullable: false
                                nullable: false

image

The documentation here for OpenAPI specification 3.0 describes structures that look much simpler than this one:
https://swagger.io/docs/specification/data-models/dictionaries/

@tyzhnenko
Copy link
Contributor Author

@RobertoPrevato Oh, I forgot about additionalProperties, thanks for the example. The idea was to also add type validation(via regex) for keys in dicts. The regex looks ugly, but I couldn't find another way to validate a type for keys.

I'll prepare changes with additionalProperties. I think it will look much better

@tyzhnenko
Copy link
Contributor Author

it requires Neoteroi/essentials-openapi#43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants