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

Duplicate Fields Serialized to Request Body When Set Explicitly to Null #2768

Open
mslogriffi opened this issue Dec 10, 2024 · 2 comments
Open
Labels

Comments

@mslogriffi
Copy link

Describe the bug

I am using the GraphServiceClient class in some unit tests. Some unit tests populate fields on Graph contract objects with ternary statements (e.g. application.Description = condition ? "a description" : null). In the null cases here, I am observing the GraphServiceClient serialize the nulled field twice in the request body, which is causing JSON validation errors in my tests.

Expected behavior

Setting any field on the Application object explicitly to null and then using it in a GraphServiceClient request body serializes the object into valid JSON (without any duplicate fields).

How to reproduce

This code...

var appDisplayName = $"Application {guid}";

var application = new Application
{
    DisplayName = appDisplayName,
    Description = null,
    IdentifierUris = new List<string> { $"urn:{appDisplayName}" },
};

var createdApplication = await this.graphClient.Applications.PostAsync(application);

...Sends this request body to the Graph service the client is pointed to:

{
    "description":null,
    "description":null,
    "@odata.type":"#microsoft.graph.application",
    "displayName":"Application f5b18965-f282-455b-9d0f-cc9d816cebcf",
    "identifierUris":["urn:Application f5b18965-f282-455b-9d0f-cc9d816cebcf"]
}

This also happens with other fields in the Application class when set explicitly to null.

SDK Version

5.65.0

Latest version known to work for scenario above?

5.56.0

Known Workarounds

No response

Debug output

Click to expand log ```
</details>


### Configuration

- OS: Windows 11
- architecture: x64

### Other information

_No response_
@mslogriffi mslogriffi added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Dec 10, 2024
@andrueastman
Copy link
Member

Thanks for raising this @mslogriffi

Out of curiosity, do you still get duplicate fields if you use the following the methods to serialize the object?
https://learn.microsoft.com/en-us/openapi/kiota/serialization?tabs=csharp#serialization-helpers

@andrueastman andrueastman added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Dec 10, 2024
@mslogriffi
Copy link
Author

Thanks for raising this @mslogriffi

Out of curiosity, do you still get duplicate fields if you use the following the methods to serialize the object? https://learn.microsoft.com/en-us/openapi/kiota/serialization?tabs=csharp#serialization-helpers

It doesn't look like I do on 5.65.0. Running this code:

var guid = Guid.NewGuid().ToString("D");
var appDisplayName = $"Application {guid}";
testOutputHelper.WriteLine($"Create application '{appDisplayName}'.");

var application = new Application
{
    DisplayName = appDisplayName,
    Description = null,
    IdentifierUris = new List<string> { $"urn:{appDisplayName}" },
};

var applicationSerialized = await KiotaJsonSerializer.SerializeAsStringAsync(application);

testOutputHelper.WriteLine($"{nameof(applicationSerialized)}:{Environment.NewLine}{applicationSerialized}");

var applicationDeserialized = await KiotaJsonSerializer.DeserializeAsync(applicationSerialized, Application.CreateFromDiscriminatorValue);

testOutputHelper.WriteLine($"{nameof(applicationDeserialized)}:{Environment.NewLine}{applicationDeserialized.ToJson()}");

Gives this result (with 'testOutputHelper' being a ITestOutputHelper given by XUnit):

applicationSerialized:
{"description":null,"@odata.type":"#microsoft.graph.application","displayName":"Application 9d9bac23-fb27-432b-bb76-20d17bf59d73","identifierUris":["urn:Application 9d9bac23-fb27-432b-bb76-20d17bf59d73"]}
applicationDeserialized:
{"AddIns":null,"Api":null,"AppId":null,"ApplicationTemplateId":null,"AppManagementPolicies":null,"AppRoles":null,"Certification":null,"CreatedDateTime":null,"CreatedOnBehalfOf":null,"DefaultRedirectUri":null,"Description":null,"DisabledByMicrosoftStatus":null,"DisplayName":"Application 9d9bac23-fb27-432b-bb76-20d17bf59d73","ExtensionProperties":null,"FederatedIdentityCredentials":null,"GroupMembershipClaims":null,"HomeRealmDiscoveryPolicies":null,"IdentifierUris":["urn:Application 9d9bac23-fb27-432b-bb76-20d17bf59d73"],"Info":null,"IsDeviceOnlyAuthSupported":null,"IsFallbackPublicClient":null,"KeyCredentials":null,"Logo":null,"NativeAuthenticationApisEnabled":null,"Notes":null,"Oauth2RequirePostResponse":null,"OptionalClaims":null,"Owners":null,"ParentalControlSettings":null,"PasswordCredentials":null,"PublicClient":null,"PublisherDomain":null,"RequestSignatureVerification":null,"RequiredResourceAccess":null,"SamlMetadataUrl":null,"ServiceManagementReference":null,"ServicePrincipalLockConfiguration":null,"SignInAudience":null,"Spa":null,"Synchronization":null,"Tags":null,"TokenEncryptionKeyId":null,"TokenIssuancePolicies":null,"TokenLifetimePolicies":null,"UniqueName":null,"VerifiedPublisher":null,"Web":null,"DeletedDateTime":null,"AdditionalData":{},"BackingStore":{"ReturnOnlyChangedValues":false,"InitializationCompleted":true},"Id":null,"OdataType":"#microsoft.graph.application"}

I verified the same output is produced with version 5.56.0.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants