-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unmapped member handling during deserialization (#176)
- Loading branch information
1 parent
d4fe243
commit 3edac3e
Showing
5 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) The LEGO Group. All rights reserved. | ||
|
||
namespace LEGO.AsyncAPI.Readers | ||
{ | ||
/// <summary> | ||
/// Unmapped member handling. | ||
/// </summary> | ||
public enum UnmappedMemberHandling | ||
{ | ||
/// <summary> | ||
/// Add error to diagnostics for unmapped members. | ||
/// </summary> | ||
Error, | ||
|
||
/// <summary> | ||
/// Ignore unmapped members. | ||
/// </summary> | ||
Ignore, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ namespace LEGO.AsyncAPI.Tests | |
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using LEGO.AsyncAPI.Exceptions; | ||
using LEGO.AsyncAPI.Models; | ||
using LEGO.AsyncAPI.Models.Interfaces; | ||
|
@@ -30,6 +31,7 @@ public void Read_WithExtensionParser_Parses() | |
info: | ||
title: test | ||
version: 1.0.0 | ||
test: 1234 | ||
contact: | ||
name: API Support | ||
url: https://www.example.com/support | ||
|
@@ -57,13 +59,104 @@ public void Read_WithExtensionParser_Parses() | |
{ | ||
{ extensionName, valueExtensionParser }, | ||
}, | ||
UnmappedMemberHandling = UnmappedMemberHandling.Ignore, | ||
}; | ||
|
||
var reader = new AsyncApiStringReader(settings); | ||
var doc = reader.Read(yaml, out var diagnostic); | ||
Assert.AreEqual((doc.Channels["workspace"].Extensions[extensionName] as AsyncApiAny).GetValue<int>(), 1234); | ||
} | ||
|
||
[Test] | ||
public void Read_WithUnmappedMemberHandlingError_AddsError() | ||
{ | ||
var extensionName = "x-someValue"; | ||
var yaml = $""" | ||
asyncapi: 2.3.0 | ||
info: | ||
title: test | ||
version: 1.0.0 | ||
test: 1234 | ||
contact: | ||
name: API Support | ||
url: https://www.example.com/support | ||
email: [email protected] | ||
channels: | ||
workspace: | ||
{extensionName}: onetwothreefour | ||
"""; | ||
Func<AsyncApiAny, IAsyncApiExtension> valueExtensionParser = (any) => | ||
{ | ||
if (any.TryGetValue<string>(out var value)) | ||
{ | ||
if (value == "onetwothreefour") | ||
{ | ||
return new AsyncApiAny(1234); | ||
} | ||
} | ||
|
||
return new AsyncApiAny("No value provided"); | ||
}; | ||
|
||
var settings = new AsyncApiReaderSettings | ||
{ | ||
ExtensionParsers = new Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>> | ||
{ | ||
{ extensionName, valueExtensionParser }, | ||
}, | ||
UnmappedMemberHandling = UnmappedMemberHandling.Error, | ||
}; | ||
|
||
var reader = new AsyncApiStringReader(settings); | ||
var doc = reader.Read(yaml, out var diagnostic); | ||
diagnostic.Errors.Should().HaveCount(1); | ||
} | ||
|
||
[Test] | ||
public void Read_WithUnmappedMemberHandlingIgnore_NoErrors() | ||
{ | ||
var extensionName = "x-someValue"; | ||
var yaml = $""" | ||
asyncapi: 2.3.0 | ||
info: | ||
title: test | ||
version: 1.0.0 | ||
test: 1234 | ||
contact: | ||
name: API Support | ||
url: https://www.example.com/support | ||
email: [email protected] | ||
channels: | ||
workspace: | ||
{extensionName}: onetwothreefour | ||
"""; | ||
Func<AsyncApiAny, IAsyncApiExtension> valueExtensionParser = (any) => | ||
{ | ||
if (any.TryGetValue<string>(out var value)) | ||
{ | ||
if (value == "onetwothreefour") | ||
{ | ||
return new AsyncApiAny(1234); | ||
} | ||
} | ||
|
||
return new AsyncApiAny("No value provided"); | ||
}; | ||
|
||
var settings = new AsyncApiReaderSettings | ||
{ | ||
ExtensionParsers = new Dictionary<string, Func<AsyncApiAny, IAsyncApiExtension>> | ||
{ | ||
{ extensionName, valueExtensionParser }, | ||
}, | ||
UnmappedMemberHandling = UnmappedMemberHandling.Ignore, | ||
}; | ||
|
||
var reader = new AsyncApiStringReader(settings); | ||
var doc = reader.Read(yaml, out var diagnostic); | ||
diagnostic.Errors.Should().HaveCount(0); | ||
} | ||
|
||
[Test] | ||
public void Read_WithThrowingExtensionParser_AddsToDiagnostics() | ||
{ | ||
|