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

[Python] Initial fixes using simple oneOfs with discriminator #4435

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

andreaTP
Copy link
Contributor

@andreaTP andreaTP commented Apr 2, 2024

Fixes #4433

@andreaTP andreaTP requested a review from a team as a code owner April 2, 2024 18:41
@andreaTP

This comment was marked as outdated.

@andreaTP andreaTP marked this pull request as draft April 3, 2024 15:01
@andreaTP andreaTP force-pushed the initial-fixes-simple-oneOf-py branch from 3a15f0a to 5112a5a Compare April 9, 2024 17:29
@andreaTP andreaTP marked this pull request as ready for review April 9, 2024 18:07
{
// Prevent duplicates
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those changes are fixing #4178

Please note that two properties with colliding serialization names are going to collide anyway causing just an even more subtle runtime error.
I believe this is an improvement that doesn't impact code that was already correct, but it would be great to test the changes on the Graph API to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@andreaTP The PR looks good from my end. I just need to run some tests on the graph api metadata and I'll confirm if everything is good here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🙏 thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Looking at microsoftgraph/msgraph-sdk-dotnet@dev...kiota/v1.0/pipelinebuild/142930

It looks like there's something we need to rethink here. It looks like kiota expects the property to be added to the codeDom either way if it exists up the hierarchy.

The relevant writer/refiner of the language will then use the ExistsInBaseType property to determine whether the property should be redifined in the child type based on the language semantics. It also allows the property to hold multiple values based on the context(if we are in the base or child class) in scenarios for default values.

Copy link
Member

Choose a reason for hiding this comment

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

The groupby here is fine, it'd prevent somebody from adding twice the same property from the same schema. Unlikely because of the conventions of JSON schema, but fine.
What's really problematic here is this if block, it effectively breaks behaviour with overridden defaults in derived types. Can you provide more context on why do you think it's necessary in an oneOf scenario @andreaTP please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the check, a discriminator property is added multiple times (as reported in other issues) and it breaks serialization/deserialization.

Copy link
Member

Choose a reason for hiding this comment

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

and that discriminator property being added multiple times only happens whenever we have a union type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know, it happens for sure when we have a union type, if this is the only case I cannot tell.
IIRC the discriminator gets added +2 times anytime the schema declaring it appears in the OpenAPI definition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@andreaTP
Copy link
Contributor Author

andreaTP commented Apr 9, 2024

@baywet @andrueastman to your attention as I hope we can get this one(or the resolution of the linked issues in another form) at your earliest 🙏

@andrueastman andrueastman self-assigned this Apr 11, 2024
@andreaTP
Copy link
Contributor Author

The "build" failure is correct as I haven't finished fixing the unit tests according to the new implementation 😅 doing it now.

@andreaTP
Copy link
Contributor Author

Moving back to draft as I need a little guidance on the failing tests, I think that I'm missing an invariant, but struggling to get it right for all the use cases.

@andreaTP andreaTP marked this pull request as draft April 11, 2024 17:40
@andreaTP
Copy link
Contributor Author

if anyone of the maintainers is available to point me in the right direction or helping out it would be much appreciated 🙏

@andrueastman
Copy link
Member

if anyone of the maintainers is available to point me in the right direction or helping out it would be much appreciated 🙏

#4435 (comment)

@andreaTP
Copy link
Contributor Author

Apologize I forgot to follow up, @baywet I think that I do not fully understand all the use cases, it will be great if you can look into this 🙏

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for starting this.
@andreaTP Do you have any specific question regarding the guidance you're looking for here? Through reviewing the issue and the PR I couldn't see any outstanding questions?

@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed Csharp code generation to put braces on new lines (where it makes sense). [#4347](https://github.com/microsoft/kiota/issues/4347)
- Fixed a bug where some no-content status codes would be considered structured (301, 302, 303, 307) when described. [#4190](https://github.com/microsoft/kiota/issues/4190)
- TypeScript is now a preview language!
- Fixed a bug where the code generated for Python in presence of discriminator values was not compiling/working. [#4433](https://github.com/microsoft/kiota/issues/4433)
Copy link
Member

Choose a reason for hiding this comment

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

this needs to be moved to the unreleased section

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I'll do it

Comment on lines +140 to +147
var className = parentClass.Name;
if (parentClass.Parent is CodeClass pc &&
!string.IsNullOrEmpty(pc.Name) &&
pc.InnerClasses.Any(x => x == parentClass))
{
className = $"{pc.Name}.{parentClass.Name}";
}
writer.WriteLine($"{ResultVarName} = {className}()");
Copy link
Member

Choose a reason for hiding this comment

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

I'm struggling to understand what's happening here. By definition the wrapper class cannot/shouldn't be one of the member types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤷‍♂️ I just changed the code that was not compiling, not sure if there is a more principled fix for the issue.

src/Kiota.Builder/CodeDOM/CodeClass.cs Outdated Show resolved Hide resolved
Co-authored-by: Vincent Biret <vibiret@microsoft.com>
@andreaTP
Copy link
Contributor Author

Do you have any specific question regarding the guidance you're looking for here? Through reviewing the issue and the PR I couldn't see any outstanding questions?

@baywet thanks for getting back on this!
Some Unit tests are breaking too, but I'm not sure I understand what's the desired behavior in all the cases.
As demonstrated here: #4435 (comment) this PR is breaking "something" but I'm unable to understand what and how to fix it.

If you can have a look at the failing unit tests you can probably give me some answers ...

@baywet
Copy link
Member

baywet commented Apr 24, 2024

@andreaTP I fixed all the failing unit tests by simply removing the if block I was referring to here but didn't push my changes as I wanted to get more context on the matter first.
I think it's be important to get more context on why you think this if block is needed, and maybe narrow down the scenario so the fix can be adapted to be more specific in order to avoid undesired side effects.

@andreaTP
Copy link
Contributor Author

@baywet thanks a lot for checking this one!
Have you tested with the newly introduced Python it test?
I bet you get a failure there and we can go on from there, I assume that the generated code and a StackTrace are going to be worth a thousand words.

Alternatively, I can get back to this early next week.

@andreaTP
Copy link
Contributor Author

andreaTP commented May 7, 2024

For the records, this is still an issue as far as I can tell, but it dropped on my priority list and it would be great if someone would take over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress 🚧
Development

Successfully merging this pull request may close these issues.

[Python] Generated code for simple oneOfs doesn't work
3 participants