Project

General

Profile

Feature #46423

Feature #40907: mgr/dashboard: REST API improvements

Feature #40962: mgr/dashboard: REST API: runtime front-end/back-end (end-to-end) schema validation

mgr/dashboard: Python runtime typechecking framework

Added by Ernesto Puerta 7 months ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
dashboard/backend
Target version:
% Done:

0%

Source:
Tags:
Backport:
Reviewed:
Affected Versions:
Pull request ID:

Description

Python (as well as Typescript) lacks runtime typechecking, this may lead to a lot runtime issues. Part of this is alleviated by QA/Back-end API tests, but they cannot cover all the possible real-life situations.

Additionally, there's a lot of (potential) duplicate type definitions at both Front-end (Typescript) and Back-end (Python).

One of the most mature solutions for tackling these concerns is pydantic. For our purposes, it fulfills the following requirements:
  • Runtime type-checking: integrated with mypy.
    >>> class RBD(pydantic.BaseModel):
    ...   id: int
    ...   name: str
    ... 
    
    >>> RBD(id=1, name='test_rbd')
    RBD(id=1, name='test_rbd')
    
    >>> RBD(id='1', name='test_rbd')
    RBD(id=1, name='test_rbd')
    
    >>> RBD(id='asd', name='test_rbd')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "pydantic/main.py", line 338, in pydantic.main.BaseModel.__init__
    pydantic.error_wrappers.ValidationError: 1 validation error for RBD
    id
      value is not a valid integer (type=type_error.integer)
    
    
  • Export Python types/schemas to JSON Schema/OpenAPI: this could be later imported to Angular/Typescript via 3rd party tools
    # OpenAPI compliant
    >>> RBD.schema_json()
    '{"title": "RBD", "type": "object", "properties": {"id": {"title": "Id", "type": "integer"}, "name": {"title": "Name", "type": "string"}}, "required": ["id", "name"]}'
    

As a con, current package support is limited to latest upstream distros *


Related issues

Related to mgr - Feature #46501: mgr/dashboard: evaluate FastAPI New

History

#1 Updated by Ernesto Puerta 6 months ago

Also available in: Atom PDF