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

fix: resolving wrong reference #180

Merged
merged 1 commit into from
Jun 14, 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
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
Loading