Skip to content

ProjectLabel objects are not JSON serializable #1116

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

Closed
MatthieuPERIN opened this issue Jun 12, 2020 · 4 comments · Fixed by #1872
Closed

ProjectLabel objects are not JSON serializable #1116

MatthieuPERIN opened this issue Jun 12, 2020 · 4 comments · Fixed by #1872

Comments

@MatthieuPERIN
Copy link

Description of the problem, including code/CLI snippet

Idea is to use Gitlab-Python to manage Labels in numerous project in a more scallable way 😅

MWE

# setup gl-py
gl = gitlab.Gitlab(GITLAB_ADRESS, private_token=TOKEN, ssl_verify=False)
gl.auth()

#get the project
src_prj = gl.projects.get(SRC_PRJ_ID)

# try to serialize the first Label Project
with open('data.txt', 'w') as outfile:
    json.dump(src_prj.labels.list(all=True)[0], outfile)

Get the following

Traceback (most recent call last):
  File "C:/Users/MPERIN/Git/labelautomation/labelmodel.py", line 79, in <module>
    json.dump(src_prj.labels.list(all=True)[0], outfile)
  File "C:\Users\MPERIN\AppData\Local\Continuum\miniconda3\envs\labelautomation\lib\json\__init__.py", line 179, in dump
    for chunk in iterable:
  File "C:\Users\MPERIN\AppData\Local\Continuum\miniconda3\envs\labelautomation\lib\json\encoder.py", line 438, in _iterencode
    o = _default(o)
  File "C:\Users\MPERIN\AppData\Local\Continuum\miniconda3\envs\labelautomation\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ProjectLabel is not JSON serializable

Expected Behavior

The Gilabl-python object are well serialized into JSON

Actual Behavior

The serialization crashes 😒

Specifications

  • python-gitlab version: 2.2 (pip)
  • API version you are using (v3/v4): v4
  • Gitlab server version (or gitlab.com): 11.10.0
@max-wittig
Copy link
Member

You can always request the .attributes of an objects. That should be json serializable, but I guess it would make sense for all projects to be serializable.

Btw. your GitLab version is not supported by python-gitlab and it's dangerous to use. No security whatsoever, all vulnerabilities of it are public. You should upgrade to 13.0.0

@MatthieuPERIN
Copy link
Author

@max-wittig Thanks for the solution on .attributes and yes a "builtin" solution would be nicer

Agree for the GitLab version but this is a corporate internal-only use forge pending upgrade to 13.X pending (long-due) hardware migration (always the same music it seems).

@max-wittig
Copy link
Member

@MatthieuPERIN Maybe you should raise some security concerns and include this page with all vulnabilities 😄 https://hackerone.com/gitlab/hacktivity

@JohnVillalovos JohnVillalovos changed the title ProjetcLabel object are not JSON serializable ProjectLabel objects are not JSON serializable Feb 2, 2022
@JohnVillalovos
Copy link
Member

JohnVillalovos commented Feb 2, 2022

I have proposed #1872 that would allow you to serialize the objects.

If merged it would allow:

# try to serialize the first Label Project
with open('data.txt', 'w') as outfile:
    json.dump(src_prj.labels.list(all=True)[0].asdict(), outfile)

Maybe ignore this for now as I was not aware of attributes property...

JohnVillalovos added a commit that referenced this issue Jul 6, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 6, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 6, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 6, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 9, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 9, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 12, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 18, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
JohnVillalovos added a commit that referenced this issue Jul 20, 2022
Add an `asdict()` method that returns a dictionary representation copy
of the Gitlab Object. This is a copy and changes made to it will have
no impact on the Gitlab Object.

The `asdict()` method name was chosen as both the `dataclasses` and
`attrs` libraries have an `asdict()` function which has the similar
purpose of creating a dictionary represenation of an object.

Also add a `to_json()` method that returns a JSON string
representation of the object.

Closes: #1116
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants