Midnighter / structurizr-python

A Python 3 port of Simon Brown's Structurizr diagrams-as-code architecture description tool.
https://structurizr.com/
Apache License 2.0
65 stars 16 forks source link

Pydantic field validations should not be stricter than the Structurizr server #40

Open yt-ms opened 3 years ago

yt-ms commented 3 years ago

Problem description

With a new workspace in the Structurizr UI, many fields are optional (may be left blank). The model we have using Pydantic is more strict than this, which means that trying to get the workspace fails on deserialisation.

Code Sample

The stacktrace below is from creating a new workspace with a system context view that has no description. There may well be a host of other fields with the same problem.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\users\xxx\dev\structurizr-python\src\structurizr\api\structurizr_client.py", line 145, in get_workspace
    return Workspace.hydrate(WorkspaceIO.parse_raw(response.text))
  File "pydantic\main.py", line 478, in pydantic.main.BaseModel.parse_raw
  File "pydantic\main.py", line 455, in pydantic.main.BaseModel.parse_obj
  File "pydantic\main.py", line 346, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for WorkspaceIO
views -> systemContextViews -> 0 -> description
  field required (type=value_error.missing)

Context

``` System Information ================== OS Windows OS-release 10 Python 3.7.5 Package Versions ================ depinfo 1.5.4 httpx 0.15.3 importlib_metadata 1.7.0 ordered-set 3.1 pip 20.2.4 pydantic 1.6.1 python-dotenv 0.14.0 setuptools 41.2.0 structurizr-python 0.1.0+1.g58d76cb ```
Midnighter commented 3 years ago

Best would be a systematic comparison with the OpenAPI schema, I suppose. https://editor.swagger.io/?url=https://raw.githubusercontent.com/structurizr/json/master/structurizr.yaml

yt-ms commented 3 years ago

Best would be a systematic comparison with the OpenAPI schema, I suppose. https://editor.swagger.io/?url=https://raw.githubusercontent.com/structurizr/json/master/structurizr.yaml

Unless I'm missing something, none of the properties are marked as required in the YAML!

Midnighter commented 3 years ago

Hah, fair enough. That's probably because the Java classes can be empty instances. However, for some measure of sanity checking, I chose to add a few required arguments on the classes which means those keys should also be required by pydantic. However, if everything is empty, class instantiation should probably just be ignored.