-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Convert JSON or Dict to client objects #977
Comments
/assign @roycaihw |
I was just trying to do this. This would be a useful function. |
@chekolyn could you take this issue, and work on a pr? |
@yliaog sure, I'll give it a shot. i think the deserializer() might have been auto generated by swagger codegen. So where would this be appropriate? utils? |
Yes that looks like generated code. I think utils should be fine, but somewhere in the base repo may work as well since we don’t necessarily require e2e tests on this one (do we?) |
I've been doing some tests and using a similar approach to utils.create_from_yaml to infer the object response_type to use in the already generated codegen deserialize function. Using this approach is possible to deserialize single objects and a list of multiple items to a list of individual namespaced objects; however this not covered all the different deserialize options we have.
My question is is there a need to deserialize to these type of objects? Or deserializing to single objects covers the use case? One complication of this is when the is a List with multiple kind of objects (something like json output of @remram44 will the proposed solution work for you? |
I'm not sure what you mean, what kind of lists wouldn't be deserializable? |
Is really more about the object we output and not the input. For example:
So in the proposed solution we would output a native python list/array that has individually deserialize objects. Something like this:
The alternative is to separate the list items by kind and return another
The reason I mentioned this is that they way we would iterate over the items would be different. |
Allows loading objects from json and yaml files. See for feature request kubernetes-client#977
I see there is a PR already open for this functionality. I'm not sure if the discussion is better had here or on the PR directly. There has been talk over on the apache-airflow repo of how useful the private deserialize methods would be exposed as public. It seems to me that the way the private method approaches this is the simplest, leaving it to the caller to also pass in the |
@davlum if I understand correctly, you would like to pass the object type directly; perhaps we can pass it along and not infer it, basically skip this line: https://github.com/chekolyn/python/blob/json_deserialize/kubernetes/utils/deserialize.py#L252 Additionally, we look for more than just the kind to infer an object. We look for the api version and api group. Do you have any json/yaml examples I can test with? |
Yeah, I'm essentially looking for the functionality of
|
@davlum i think a klaas/response_type str parameter in here https://github.com/chekolyn/python/blob/json_deserialize/kubernetes/utils/deserialize.py#L238 would do pass the klass to the __deserialize_model via this line: python/kubernetes/client/api_client.py Line 289 in ca4f311
I will amend/add this tonight. This is still work in progress and I thank you for the suggestion. |
Passing in the |
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-testing, kubernetes/test-infra and/or fejta. |
/remove-lifecycle stale |
Any mouvement on this? |
Alright, I saw that #574 (comment) works... It's awkward, but works... |
@davlum try the method at #574 (comment) works... I needed to clone objects and change the names and properties. So I had it transformed into a |
That's not quite what I'm looking for. I would like to go from JSON/YAML to client objects defined within this library. If I'm not mistaken your example returns JSON from the client, modifies it and writes back YAML, never making the conversion from YAML -> client object. |
Reading deserialize, it mostly needs the |
That's how I have to do it now: class FakeKubeResponse:
def __init__(self, obj):
import json
self.data = json.dumps(obj)
fake_kube_response = FakeKubeResponse(initial_object_json)
v1pod = api_client.deserialize(fake_kube_response, 'V1Pod') Will be glad to remove this hack after this issue close. |
@piroszhog I think that is the right workaround at the moment, and this issue should be closed once #989 is finished. |
To make this hack work for me, I had to add explicit
|
the origin PR #989 is closed, so can we reopen this issue? |
/reopen |
@pigletfly: You can't reopen an issue/PR unless you authored it or you are a collaborator. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
👍 if this could be reopened. Would be very useful |
Open another duplicate, it's what I did. If they'd rather create all this noise and duplication by using stupid bots, we must play along. |
/reopen |
@yliaog: Reopened this issue. In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/remove-lifecycle rotten |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
/remove-lifecycle rotten |
Hi, I'm interested in contributing to this issue. How can I get started? |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
The project might be rotten but the issue is fresh /remove-lifecycle stale |
Sorry if I didn't use the right incantation to scare away the bots /remove-lifecycle rotten |
The Kubernetes project currently lacks enough contributors to adequately respond to all issues. This bot triages un-triaged issues according to the following rules:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
See also #340 (comment) (was closed for inactivity, NOT resolved)
See also #63
Motivation
It is sometimes useful to be able to load Python objects from JSON, for example to read it from a config file. It should be possible to get Python objects from JSON, to pass around the code in a type-safe manner, for modification and ultimately sending through the client.
Feature request
There should be functions to convert JSON to objects and objects to JSON. This code already exists (it's required to talk to the API server), it should be exposed.
Workaround?
Right now, you can put this raw JSON into Python objects' fields, and the API client will apparently accept this, however if you use such mixed objects your code will probably break since their attribute names are different (API uses PascalCase, Python objects use snake_case). Even if taking care to handle that, you will reach the point where you have to handle things coming from the API client (Python objects using snake_case) differently from what you load from config files (JSON using PascalCase). Really messy.
Right now I am left considering dropping this lib altogether and issuing requests directly, since the point of the wrapper is to expose safe typed Python classes instead of the raw JSON, but this doesn't work if you use any kind of config file.
Current status
It is somewhat possible to convert from a Python object to JSON using
client.api_client.sanitize_for_serialization()
, however converting from JSON to Python is completely impossible; thedeserialize()
method that exists expects arequests.Response
, not JSON. Callingclient.api_client._ApiClient__deserialize()
works, but that is a private method, and you need to pass it thekind
read from the JSON yourself.Apologies for duplicating #340 but I cannot re-open it myself.
The text was updated successfully, but these errors were encountered: