Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Comments

Close side panel

Adds support for Copy Constructors#853

Merged
MaggieKimani1 merged 14 commits intovnextmicrosoft/OpenAPI.NET:vnextfrom
mk/add-copy-constructors-for-modelsmicrosoft/OpenAPI.NET:mk/add-copy-constructors-for-modelsCopy head branch name to clipboard
Jul 12, 2022
Merged

Adds support for Copy Constructors#853
MaggieKimani1 merged 14 commits intovnextmicrosoft/OpenAPI.NET:vnextfrom
mk/add-copy-constructors-for-modelsmicrosoft/OpenAPI.NET:mk/add-copy-constructors-for-modelsCopy head branch name to clipboard

Conversation

@MaggieKimani1
Copy link
Contributor

Fixes #832

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.

wouldn't it make more sense to implement ICloneable semantically instead?
Also I noticed that in every copy constructor the code is just copying the reference for properties, instead of making a copy of the object. That means that a change on myOriginal.PropA.PropAlpha = "foo" would also impact the copy myCopy.PropA.PropAlpha is now also valued at "foo". This could have tons of side effects.
See how I implemented it for Kiota https://github.com/microsoft/kiota/blob/d19d14e06bc6fcadac91d208d2bf1c54c1a8918c/src/Kiota.Builder/CodeDOM/CodeMethod.cs#L160

@MaggieKimani1
Copy link
Contributor Author

wouldn't it make more sense to implement ICloneable semantically instead? Also I noticed that in every copy constructor the code is just copying the reference for properties, instead of making a copy of the object. That means that a change on myOriginal.PropA.PropAlpha = "foo" would also impact the copy myCopy.PropA.PropAlpha is now also valued at "foo". This could have tons of side effects. See how I implemented it for Kiota https://github.com/microsoft/kiota/blob/d19d14e06bc6fcadac91d208d2bf1c54c1a8918c/src/Kiota.Builder/CodeDOM/CodeMethod.cs#L160

Do you mind expounding on the side effects?
Also implementing ICloneable semantically as you've done in Kiota would require type-casting when one is trying to mutate the cloned object?

@matt9ucci
Copy link
Contributor

Seems to be related with #537. I hope ICloneable, no side effects and no hierarchy problems, is implemented for all models.

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 making updates, still a few changes required

src/Microsoft.OpenApi/Models/OpenApiError.cs Show resolved Hide resolved
src/Microsoft.OpenApi/Models/OpenApiCallback.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Models/OpenApiCallback.cs Outdated Show resolved Hide resolved
@darrelmiller
Copy link
Member

The API design that was approved for minimal APIs was to be able to use copy constructors. By having copy constructors it allows creating a new instance of the object and then using object initializers to set values. e.g.

app.MapGet("/foo", () => {})
.WithOpenApi(generatedOperation => new(generatedOperation) {
  OperationId = "MadeByMe"
});

It is not possible to do the same with ICloneable. Can we switch this to use copy constructors?

Also, it is not necessary to clone strings. Strings are immutable and so there is no harm in having the clone share a reference to the string. If one of the properties is updated, it will not affect the other.

@baywet
Copy link
Member

baywet commented May 17, 2022

sorry for the confusion, I didn't have the additional context on this one.

@MaggieKimani1
Copy link
Contributor Author

Thank you for the clarification. I'll revert the PR to the state where I had implemented the copy constructors.

@MaggieKimani1 MaggieKimani1 force-pushed the mk/add-copy-constructors-for-models branch from a35ab9c to c61e1cb Compare May 23, 2022 11:11
src/Microsoft.OpenApi/Models/OpenApiCallback.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Models/OpenApiMediaType.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Models/OpenApiPaths.cs Show resolved Hide resolved
src/Microsoft.OpenApi/Models/OpenApiLicense.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiArray.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/IOpenApiAny.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Models/OpenApiExample.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiArray.cs Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiNull.cs Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiObject.cs Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs Show resolved Hide resolved
src/Microsoft.OpenApi/Any/IOpenApiAny.cs Outdated Show resolved Hide resolved
src/Microsoft.OpenApi/Any/OpenApiPrimitive.cs Outdated Show resolved Hide resolved
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.

Add support for copy constructors to Microsoft.OpenApi.Models objects

4 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.