Skip to content
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

Inconsistent resource objects when output format is json #461

Closed
MarkusFreitag opened this issue Mar 7, 2023 · 2 comments · Fixed by #622
Closed

Inconsistent resource objects when output format is json #461

MarkusFreitag opened this issue Mar 7, 2023 · 2 comments · Fixed by #622

Comments

@MarkusFreitag
Copy link
Contributor

MarkusFreitag commented Mar 7, 2023

When setting the output format to json, the resource object is rendered differently between the corresponding list and describe command.

The following json output is just an example, this behaviour is not limited to the iso resource.

# list all isos
[{"id":1,"name":"dvd_a.iso","description":"DVD A","type":"","deprecated":"0001-01-01T00:00:00Z"},{"id":2,"name":"","description":"cd_b.iso","type":"","deprecated":"0001-01-01T00:00:00Z"}]

# describe iso 1
{"deprecated":null,"description":"DVD A","id":1,"name":"dvd_a.iso","type":"public"}

# describe iso 2
{"deprecated":null,"description":"cd_b.iso","id":2,"name":null,"type":"private"}

After digging a bit into this, I noticed that a ListCmd has a JSONSchema attribute which converts the resource into a hcloud-go api schema object before decoding it into a JSON string. In contrast the DescribeCmd just decodes the resource object directly.

@github-actions
Copy link
Contributor

github-actions bot commented Jun 6, 2023

This issue has been marked as stale because it has not had recent activity. The bot will close the issue if no further action occurs.

@github-actions github-actions bot added the stale label Jun 6, 2023
@apricote apricote removed the stale label Jun 6, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Sep 5, 2023

This issue has been marked as stale because it has not had recent activity. The bot will close the issue if no further action occurs.

@github-actions github-actions bot added the stale label Sep 5, 2023
@jooola jooola added pinned and removed stale labels Sep 5, 2023
phm07 added a commit to hetznercloud/hcloud-go that referenced this issue Dec 12, 2023
This PR adds automatic generation of schema conversion methods in
`schema.go` with [goverter](https://github.com/jmattheis/goverter).

Previously, `schema.go` was manually written. This is a tedious task and
is also prone to errors.

This PR also adds new methods to convert `hcloud` objects back into
schemas. This allows for round trip testing (converting `schema ->
hcloud -> schema` or `hcloud -> schema -> hcloud` and testing for
equality) and allows for removal of redundant code in
[hcloud/cli](https://github.com/hetznercloud/cli).

Since the round trip tests ensure that a schema converts into an
`hcloud` object and back, formatting will be consistent between
conversions. This would fix issues like
hetznercloud/cli#461.
phm07 added a commit to hetznercloud/hcloud-go that referenced this issue Dec 12, 2023
In some places the schema definitions are not consistent with the API
documentation, for example properties may not have pointer receivers
even though they are marked as optional in the documentation. This
results in inconsistent JSON when converting `JSON -> Schema -> JSON`
(as seen for example in hetznercloud/cli#461,
where one output is the exact API response and one is serialized and
then converted back to JSON).

This PR aims to fix this issue by making the schema definitions
consistent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants