Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.86k stars 78 forks source link

Support mapping types in OpenAPI schema #476

Open tyzhnenko opened 8 months ago

tyzhnenko commented 8 months ago

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"]
    }
  }
}
tyzhnenko commented 8 months ago

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 commented 8 months ago

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 :warning:

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

Additional details and impacted files ```diff @@ 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 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

RobertoPrevato commented 8 months ago

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 commented 8 months ago

@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 commented 8 months ago

it requires https://github.com/Neoteroi/essentials-openapi/pull/43