Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Parse empty strings as nullable Guid #214

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.3] - 2024-04-25

### Changed

- Parse empty strings as nullable Guid

## [1.2.2] - 2024-04-19

### Changed
Expand Down
17 changes: 17 additions & 0 deletions Microsoft.Kiota.Serialization.Json.Tests/JsonParseNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ public void ParseGuidWithoutConverter()
Assert.Equal(id, entity.Id);
}

[Fact]
public void ParseGuidEmptyString()
{
// Arrange
var json = $"{{\"id\": \"\"}}";
var serializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.General);
var serializationContext = new KiotaJsonSerializationContext(serializerOptions);
using var jsonDocument = JsonDocument.Parse(json);
var rootParseNode = new JsonParseNode(jsonDocument.RootElement, serializationContext);

// Act
var entity = rootParseNode.GetObjectValue(_ => new ConverterTestEntity());

// Assert
Assert.Null(entity.Id);
}

[Fact]
public void GetEntityWithUntypedNodesFromJson()
{
Expand Down
16 changes: 13 additions & 3 deletions src/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,19 @@ public JsonParseNode(JsonElement node, KiotaJsonSerializationContext jsonSeriali
/// Get the guid value from the json node
/// </summary>
/// <returns>A guid value</returns>
public Guid? GetGuidValue() => _jsonNode.ValueKind == JsonValueKind.String
? _jsonNode.Deserialize(_jsonSerializerContext.Guid)
: null;
public Guid? GetGuidValue()
{
if(_jsonNode.ValueKind != JsonValueKind.String)
return null;

if(_jsonNode.TryGetGuid(out var guid))
return guid;

if(string.IsNullOrEmpty(_jsonNode.GetString()))
return null;

return _jsonNode.Deserialize(_jsonSerializerContext.Guid);
}

/// <summary>
/// Get the <see cref="DateTimeOffset"/> value from the json node
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.Kiota.Serialization.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<VersionPrefix>1.2.2</VersionPrefix>
<VersionPrefix>1.2.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Loading