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

Create valid property identifiers when generating OData client #333

Merged

Conversation

gregwinterstein
Copy link
Contributor

@gregwinterstein gregwinterstein commented Feb 2, 2023

@gregwinterstein gregwinterstein force-pushed the fix/317-create-valid-identifiers branch from 280d032 to 108d251 Compare February 22, 2023 21:31
Copy link

@SomeTroglodyte SomeTroglodyte left a comment

Choose a reason for hiding this comment

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

Simple - this can't be any worse than the current master.

@gregwinterstein
Copy link
Contributor Author

@microsoft-github-policy-service agree

@mvarie
Copy link

mvarie commented Apr 13, 2023

When using the ODataConnectedService extension in Visual Studio 2022 to generate C# classes from an unpatched Dynamics 365 10.0.29 OData ($metadata) url, @gregwinterstein 's fix has generated code that compiles correctly.

Note: by unpatched I mean that our Dynamics 365 has no hotfix for LCS 753226, nor it can be applied - if you have 10.0.30 or 10.0.31 you can apply it, while 10.0.32+ is supposed to incorporate the fix.

@SomeTroglodyte
Copy link

@gregwinterstein 's fix has generated code

I'd like to test with non-dynamics sources but am too lazy to compile from source - care to share a build?

@mvarie
Copy link

mvarie commented Apr 14, 2023

@gregwinterstein gregwinterstein force-pushed the fix/317-create-valid-identifiers branch from 108d251 to 0d3e015 Compare June 6, 2023 17:41
@gregwinterstein gregwinterstein marked this pull request as draft June 13, 2023 18:57
@gregwinterstein gregwinterstein marked this pull request as ready for review June 14, 2023 23:54
@gregwinterstein gregwinterstein force-pushed the fix/317-create-valid-identifiers branch from bfcc9b4 to 8a64678 Compare June 15, 2023 17:49
@SomeTroglodyte
Copy link

Took me a while, but I have just now built @gregwinterstein 's "all-fixes" branch from source and I can confirm it generates a working connected service where the current public version fails miserably. A source feed with dashes in field names no longer lets the compiler stumble over all the invalid subtractions...

Sample excerpt
        /// <summary>
        /// There are no comments for Property _SMC_K in the schema.
        /// </summary>
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "#VersionNumber#")]
        [global::Microsoft.OData.Client.OriginalNameAttribute("_SMC-K")]
        public virtual string _SMC_K
        {
            get
            {
                return this.__SMC_K;
            }
            set
            {
                this.On_SMC_KChanging(value);
                this.__SMC_K = value;
                this.On_SMC_KChanged();
                this.OnPropertyChanged("_SMC-K");
            }
        }
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.OData.Client.Design.T4", "#VersionNumber#")]
        private string __SMC_K;
        partial void On_SMC_KChanging(string value);
        partial void On_SMC_KChanged();

👍 👍 👍

Copy link

@SomeTroglodyte SomeTroglodyte left a comment

Choose a reason for hiding this comment

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

Tested and found working perfectly

@jackypowell
Copy link

My team really needs these changes. Can we please get them reviewed soon? Please and thank you! @ElizabethOkerio @KenitoInc @habbes @gathogojr

@habbes
Copy link
Contributor

habbes commented Aug 18, 2023

@gathogojr you had some thoughts around this issue. Could you check this PR?

@@ -2442,7 +2444,9 @@ internal void WritePropertiesForSingleType(IEnumerable<IEdmProperty> properties)
foreach (IEdmProperty property in properties.Where(i => i.PropertyKind == EdmPropertyKind.Navigation))
{
string propertyType;
string propertyName = this.context.EnableNamingAlias ? Customization.CustomizeNaming(property.Name) : property.Name;
string propertyName = IdentifierMappings.ContainsKey(property.Name) ?
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Dictionary<TKey,TValue>.TryGetValue(TKey, TValue) method instead of ContainsKey method + Dictionary<TKey,TValue>.Item[TKey] property. TryGetValue should be more efficient and cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to TryGetValue() in a869504

@gathogojr
Copy link
Contributor

gathogojr commented Sep 25, 2023

Thank you @gregwinterstein for your contribution.
Have you considered that this could be a breaking change for someone who's REGENERATING the proxy classes?
Specifically for the case of the property named Context.

However, it might also not be a breaking change since the proxy classes that are generated in such a scenario do not compile meaning the affected customers are likely to be manually editing the proxy classes to mitigate the issue.
Either way I think it'd be prudent to try and replicate the scenario by first generating the proxy using the extension before the fix and then regenerating it after the fix.

@SomeTroglodyte
Copy link

REGENERATING

I confirm: Regenerating is the most frequent use for me. Without this PR, each run requires 1-2 minutes of manual edits afterwards, per feed. This saves time especially then regenerating.

@gregwinterstein
Copy link
Contributor Author

Have you considered that this could be a breaking change for someone who's REGENERATING the proxy classes? Specifically for the case of the property named Context.

@gathogojr, it has occurred to me that this pull request may cause some breaking changes because of the way valid identifiers are generated vs the way they were generated previously. I don't have a good solution for that.

My main issue is that I am unable to generate proxies that compile for either Dynamics F&O or Dynamics CRM (this doesn't entirely resolve all of the CRM issues due to inconsistent casing in the CRM metadata). This change generates proxies that compile for Dynmaics F&O and gets a lot closer for Dynamics CRM.

If backward compatibility is an issue, I'm open to suggestions.

Greg Winterstein and others added 25 commits April 7, 2024 10:25
Co-authored-by: Clément Habinshuti <[email protected]>
Co-authored-by: Clément Habinshuti <[email protected]>
@gathogojr gathogojr force-pushed the fix/317-create-valid-identifiers branch from 83c6c69 to 590a8f5 Compare April 7, 2024 07:25
@gathogojr gathogojr merged commit ee15aee into OData:master Apr 7, 2024
2 checks passed
@gathogojr
Copy link
Contributor

Thank you @gregwinterstein for your contribution

@gregwinterstein gregwinterstein deleted the fix/317-create-valid-identifiers branch April 8, 2024 18:07
@SomeTroglodyte
Copy link

SomeTroglodyte commented Jul 2, 2024

It might be nice to have some indication here on when this change might become available all the way through normal Visual Studio updates. 2025? 2026?

Edit: This is already available through VS marketplace in 1.1.0:

ChangeLog:
1.1.0
Fix bug affecting excluding of schema types, operations, and operation imports.
Support omitting runtime version and code generation timestamp from generated files.
Fix bug affecting emitting of dynamic properties container property.
Support arm64 product architecture.
Support generation of valid property identifiers when service metadata does not strictly adhere to OData specification on property name.

I just got thrown off because that is not mirrored here on the releases page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants