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

Support encode int as string #2771

Merged
merged 15 commits into from
Aug 29, 2024
Merged

Support encode int as string #2771

merged 15 commits into from
Aug 29, 2024

Conversation

msyyc
Copy link
Member

@msyyc msyyc commented Aug 14, 2024

fix #2703

pending on Azure/cadl-ranch#656

@@ -50,7 +50,8 @@ export function pyright() {
}

export function eslint() {
const checkWarning = argv.skipWarning ? "" : "--max-warnings=0";
// const checkWarning = argv.skipWarning ? "" : "--max-warnings=0";
const checkWarning = "";
Copy link
Member Author

@msyyc msyyc Aug 23, 2024

Choose a reason for hiding this comment

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

skip eslint failure caused by deprecation of urlencode:
image

After #2775 merged, we could revert this change.

@@ -675,6 +677,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
Copy link
Contributor

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 have to modify model_base.py, we can pass in a deserialization function into the rest_field and have it serialize and deserialize as a string

Copy link
Member Author

Choose a reason for hiding this comment

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

class _RestField:
def __init__(
self,
*,
name: typing.Optional[str] = None,
type: typing.Optional[typing.Callable] = None, # pylint: disable=redefined-builtin
is_discriminator: bool = False,
visibility: typing.Optional[typing.List[str]] = None,
default: typing.Any = _UNSET,
format: typing.Optional[str] = None,
is_multipart_file_input: bool = False,
):

Appreciate if you could explain more how to pass in deserialization function? It seems that RestField has no parameter to pass it.

Copy link
Contributor

Choose a reason for hiding this comment

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

you pass it through the type kwarg. For example, in this test, we've typed the prop as being a sequence, but in terms of serislizstion / deserialization, we want it to be a set, so we pass in a callback. We should be able to do the same thing here, where we type it as an int, but we pass in type=lambda x: str(x) / write a callback that does that instead

Copy link
Member

Choose a reason for hiding this comment

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

for datetime/bytes encoding, we put the logic in model_base.py. shall we keep consistent for all the encoding things? another thing is if use callback, how to deal with serialization?

Copy link
Member Author

Choose a reason for hiding this comment

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

@iscai-msft I think it is better not handle this feature with call_back so that we could keep consistent for all encoding things. But your comment hint me that we could confirm the deserialize and serialize function for model property when generate code so that we could save time for model initialization and I have created #2797 to track it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since the model is vendored we can def change things, so I think we can have the rest field take in serialization and deserialization as callbacks. As @msyyc said we've already thought of passing explicit callbacks into rest field for perf improvements, so I think what we should do is do a slight redesign where we have serialization and deserialization callback inputs. When we're generating, we can generate with explicit callback inputs so we don't have to use perf time to determine which serialize / deserialize function we should use

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly. And the change of redesign for serialization/deserialization will be a little huge so I would like to do it in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I've added a separate issue for the serialization and deserialization, #2801

@msyyc msyyc requested a review from iscai-msft August 26, 2024 05:04
@msyyc msyyc marked this pull request as ready for review August 26, 2024 05:04
@msyyc msyyc requested a review from tadelesh as a code owner August 26, 2024 05:04
workingDirectory: $(Build.SourcesDirectory)/autorest.python/packages/${{parameters.folderName}}/
condition: and(succeeded(), ${{ parameters.regenerate }}, eq('${{parameters.folderName}}', 'autorest.python'))

- script: |
find test/azure/generated -type f ! -name '*apiview_mapping_python.json*' -delete
find test/unbranded/generated -type f ! -name '*apiview_mapping_python.json*' -delete
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we have this code here> we should already be doing this in the regenreate.ts file, and this is also for the autorest generation. Whatever functinoality we need from this should be moved into the scripts instead of being in yml for the scripts

Copy link
Contributor

Choose a reason for hiding this comment

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

the one thing i'm not sure about is serialization. If we can't edo serialization here, we should instead add similar functionality but for serialization (and we can rename type to deserialization) instead of adding extra logic through the msrest way

Copy link
Member Author

@msyyc msyyc Aug 27, 2024

Choose a reason for hiding this comment

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

@@ -675,6 +677,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
Copy link
Contributor

Choose a reason for hiding this comment

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

you pass it through the type kwarg. For example, in this test, we've typed the prop as being a sequence, but in terms of serislizstion / deserialization, we want it to be a set, so we pass in a callback. We should be able to do the same thing here, where we type it as an int, but we pass in type=lambda x: str(x) / write a callback that does that instead

@msyyc msyyc requested a review from iscai-msft August 27, 2024 05:32
@msyyc msyyc merged commit c04063a into main Aug 29, 2024
15 checks passed
@msyyc msyyc deleted the int-encoding branch August 29, 2024 00:45
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.

Encode int as string Implementation
3 participants