Skip to content

Conversation

@tadelesh
Copy link
Member

@tadelesh tadelesh commented Jul 28, 2023

resolve #1990

model base change and test is here: Azure/azure-sdk-for-python#31028

also, fix #2034, #2035, #2036

_content = resource
else:
_content = json.dumps(resource, cls=AzureJSONEncoder) # type: ignore
_content = json.dumps(resource, cls=AzureJSONEncoder, exclude_readonly=True, exclude_none=False) # type: ignore
Copy link
Member Author

@tadelesh tadelesh Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should replace it with as_dict bc:

  1. body could be non model
  2. body could be a MutableMapping[str, Any] that mix simple dict and Model
  3. json.dumps(Model.as_dict()) will go through all models' attributes twice, it is not effiecient

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tadelesh
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

create_body_call = (
f"_{body_kwarg_name} = json.dumps({body_param.client_name}, "
"cls=AzureJSONEncoder) # type: ignore"
"cls=AzureJSONEncoder, exclude_readonly=True,\n exclude_none=False) # type: ignore"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we exclude_none True right now, it's just that it's currently handled in the JSON encoder. I feel it'll be clearer if we just pass in True here

Copy link
Member Author

@tadelesh tadelesh Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused. Let me give an example that you could help to correct:

class TestModel(Model):
    prop1: str
    prop2: str
    prop3: str
model = TestModel({"prop1": None, "prop2": NULL})
json.dumps(model, cls=AzureJSONEncoder, exclude_none=True)  # the result should be {"porp2": null}
json.dumps(model, cls=AzureJSONEncoder, exclude_none=False)  # test result should be {"prop1": null, "prop2": null}

models.update({
k: v
for k, v in sys.modules[module_end].__dict__.items()
if isinstance(v, (type, typing._GenericAlias)) # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this added code for? I don't know if we should access a private model in the typing namespace

Copy link
Member Author

@tadelesh tadelesh Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is for type alias like:

union MyNamedUnion {
  one: Model1,
  two: Model2,
}

@tadelesh tadelesh changed the title update model base update base model and add as_dict support for base model Aug 11, 2023
@iscai-msft iscai-msft enabled auto-merge (squash) August 31, 2023 19:43
@iscai-msft iscai-msft disabled auto-merge August 31, 2023 20:07
@iscai-msft iscai-msft merged commit f5074ad into main Aug 31, 2023
@iscai-msft iscai-msft deleted the as_dict branch August 31, 2023 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

version resilient model could not handle nested model in list or dict correctly Add as_dict

3 participants