Skip to content

Commit

Permalink
fix: resolving wrong reference (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean authored Jun 14, 2024
1 parent b1638bd commit 47685cd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/LEGO.AsyncAPI/Services/AsyncApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,13 @@ private T ResolveReference<T>(AsyncApiReference reference)

try
{
return this.currentDocument.ResolveReference(reference) as T;
var resolvedReference = this.currentDocument.ResolveReference(reference) as T;
if (resolvedReference == null)
{
throw new AsyncApiException($"Cannot resolve reference '{reference.Reference}' to '{typeof(T).Name}'.");
}

return resolvedReference;
}
catch (AsyncApiException ex)
{
Expand Down
27 changes: 27 additions & 0 deletions test/LEGO.AsyncAPI.Tests/AsyncApiReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace LEGO.AsyncAPI.Tests
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using FluentAssertions;
using LEGO.AsyncAPI.Exceptions;
using LEGO.AsyncAPI.Models;
using LEGO.AsyncAPI.Models.Interfaces;
Expand Down Expand Up @@ -336,6 +337,32 @@ public void Read_WithBasicPlusSecuritySchemeDeserializes()
Assert.AreEqual("Provide your username and password for SASL/SCRAM authentication", scheme.Value.Description);
}

[Test]
public void Read_WithWrongReference_AddsError()
{
var yaml =
"""
asyncapi: 2.3.0
info:
title: test
version: 1.0.0
channels:
workspace:
publish:
message:
$ref: '#/components/securitySchemes/saslScram'
components:
securitySchemes:
saslScram:
type: scramSha256
description: Provide your username and password for SASL/SCRAM authentication
""";
var reader = new AsyncApiStringReader();
var doc = reader.Read(yaml, out var diagnostic);
diagnostic.Errors.Should().NotBeEmpty();
doc.Channels.Values.First().Publish.Message.First().Should().BeNull();
}

[Test]
public void Read_WithBasicPlusOAuthFlowDeserializes()
{
Expand Down

0 comments on commit 47685cd

Please sign in to comment.