unioslo / harborapi

Python async client for the Harbor REST API v2.0.
https://unioslo.github.io/harborapi/
MIT License
28 stars 5 forks source link

Error get_replication_policies #68

Open lukonjun opened 12 months ago

lukonjun commented 12 months ago

Hi, when running get_replication_policies I run into an error

Traceback (most recent call last):
  File "main2.py", line 21, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "main2.py", line 18, in main
    replication = await client.get_replication_policies()
  File "/home/lucas/.local/lib/python3.8/site-packages/harborapi/client.py", line 1246, in get_replication_policies
    return self.construct_model(ReplicationPolicy, resp, is_list=True)
  File "/home/lucas/.local/lib/python3.8/site-packages/harborapi/client.py", line 420, in construct_model
    return [self._construct_model(cls, item) for item in data]
  File "/home/lucas/.local/lib/python3.8/site-packages/harborapi/client.py", line 420, in <listcomp>
    return [self._construct_model(cls, item) for item in data]
  File "/home/lucas/.local/lib/python3.8/site-packages/harborapi/client.py", line 432, in _construct_model
    raise e
  File "/home/lucas/.local/lib/python3.8/site-packages/harborapi/client.py", line 427, in _construct_model
    return cls.parse_obj(data)
  File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
  File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for ReplicationPolicy
filters -> 0 -> value
  value is not a valid dict (type=type_error.dict)
filters -> 1 -> value
  value is not a valid dict (type=type_error.dict)

This is the code I use

async def main() -> None:
    replication = await client.get_replication_policies()
    print(replication)

asyncio.run(main())

Any advice how to fix this, is this an api incompability with the client? I am using harbor version v2.9.0

Soopx commented 9 months ago

I get a similar error, and it appears that the reason is that the function returns a list, when the model demands a dictionary:

pydantic_core._pydantic_core.ValidationError: 2 validation errors for ReplicationPolicy filters.0.value Input should be a valid dictionary [type=dict_type, input_value='**/hello-world', input_type=str] For further information visit https://errors.pydantic.dev/2.5/v/dict_type filters.1.value Input should be a valid dictionary [type=dict_type, input_value='**', input_type=str] For further information visit https://errors.pydantic.dev/2.5/v/dict_type

It seems that both of these should be wrapped in a list, but they themselves should be dictionaries. I'm assuming this is an error with Harbor rather than the API, but it would be nice if it could be fixed somehow

Soopx commented 9 months ago

I've found the issue. In harborapi/models/model.py on line 493, it shows that "value" in the ReplicationFilter class is expecting a dictionary, but it's stored as a string. Editing this line to expect a string fixes the issue for me. Can this be tested and implemented please?