Skip to content

Commit

Permalink
remote notion of HostDocument in favour of workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Jan 29, 2025
1 parent 31f4868 commit 06abb47
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private void ResolveInternalReferences(AsyncApiDiagnostic diagnostic, AsyncApiDo
{
var reader = new AsyncApiStringReader(this.settings);

var resolver = new AsyncApiReferenceHostDocumentResolver(document);
var resolver = new AsyncApiReferenceHostDocumentResolver(this.context.Workspace);
var walker = new AsyncApiWalker(resolver);
walker.Walk(document);

Expand All @@ -210,7 +210,7 @@ private void ResolveInternalReferences(AsyncApiDiagnostic diagnostic, AsyncApiDo
private void ResolveExternalReferences(AsyncApiDiagnostic diagnostic, IAsyncApiSerializable serializable, AsyncApiDocument hostDocument)
{
var loader = this.settings.ExternalReferenceLoader ??= new DefaultStreamLoader(this.settings);
var collector = new AsyncApiRemoteReferenceCollector(hostDocument);
var collector = new AsyncApiRemoteReferenceCollector(this.context.Workspace);
var walker = new AsyncApiWalker(collector);
walker.Walk(serializable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace LEGO.AsyncAPI.Readers

internal class AsyncApiReferenceHostDocumentResolver : AsyncApiVisitorBase
{
private AsyncApiDocument currentDocument;
private AsyncApiWorkspace workspace;
private List<AsyncApiError> errors = new List<AsyncApiError>();

public AsyncApiReferenceHostDocumentResolver(
AsyncApiDocument currentDocument)
AsyncApiWorkspace workspace)
{
this.currentDocument = currentDocument;
this.workspace = workspace;
}

public IEnumerable<AsyncApiError> Errors
Expand All @@ -30,7 +30,7 @@ public override void Visit(IAsyncApiReferenceable referenceable)
{
if (referenceable.Reference != null)
{
referenceable.Reference.HostDocument = this.currentDocument;
referenceable.Reference.Workspace = this.workspace;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace LEGO.AsyncAPI.Readers.Services
internal class AsyncApiRemoteReferenceCollector : AsyncApiVisitorBase
{
private readonly List<IAsyncApiReferenceable> references = new();
private AsyncApiDocument currentDocument;
private AsyncApiWorkspace workspace;

public AsyncApiRemoteReferenceCollector(
AsyncApiDocument currentDocument)
AsyncApiWorkspace workspace)
{
this.currentDocument = currentDocument;
this.workspace = workspace;
}
/// <summary>
/// List of all external references collected from AsyncApiDocument.
Expand All @@ -36,9 +36,9 @@ public override void Visit(IAsyncApiReferenceable referenceable)
{
if (referenceable.Reference != null && referenceable.Reference.IsExternal)
{
if (referenceable.Reference.HostDocument == null)
if (referenceable.Reference.Workspace == null)
{
referenceable.Reference.HostDocument = this.currentDocument;
referenceable.Reference.Workspace = this.workspace;
}

this.references.Add(referenceable);
Expand Down
6 changes: 6 additions & 0 deletions src/LEGO.AsyncAPI/AsyncApiWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public bool Contains(string location)
return this.resolvedReferenceRegistry.ContainsKey(key) || this.artifactsRegistry.ContainsKey(key);
}

public T ResolveReference<T>(AsyncApiReference reference)
where T : class
{
return this.ResolveReference<T>(reference.Reference);
}

public T ResolveReference<T>(string location)
where T : class
{
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void SerializeV2(IAsyncApiWriter writer)
}

this.Workspace.RegisterComponents(this);
writer.Workspace = this.Workspace;

writer.RootDocument = this;
writer.WriteStartObject();

// asyncApi
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Models/AsyncApiReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public string ExternalResource
/// <summary>
/// Gets or sets the AsyncApiDocument that is hosting the AsyncApiReference instance. This is used to enable dereferencing the reference.
/// </summary>
public AsyncApiDocument HostDocument { get; set; } = null;
public AsyncApiWorkspace Workspace { get; set; } = null;

/// <summary>
/// Gets a flag indicating whether a file is a valid OpenAPI document or a fragment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ private AsyncApiChannel Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiChannel>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiChannel>(this.Reference.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -51,7 +51,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiCorrelationId Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiCorrelationId>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiCorrelationId>(this.Reference.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -46,7 +46,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private AsyncApiJsonSchema Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiJsonSchema>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiJsonSchema>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
return;
}

this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
// If Loop is detected then just Serialize as a reference.
if (!settings.LoopDetector.PushLoop(this))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiMessage Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiMessage>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiMessage>(this.Reference.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiMessageTrait Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiMessageTrait>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiMessageTrait>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -68,7 +68,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiOperationTrait Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiOperationTrait>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiOperationTrait>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -54,7 +54,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiParameter Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiParameter>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiParameter>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -48,7 +48,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private AsyncApiSecurityScheme Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiSecurityScheme>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiSecurityScheme>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -59,7 +59,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiServer Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiServer>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiServer>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -58,7 +58,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private AsyncApiServerVariable Target
{
get
{
this.target ??= this.Reference.HostDocument?.ResolveReference<AsyncApiServerVariable>(this.Reference);
this.target ??= this.Reference.Workspace?.ResolveReference<AsyncApiServerVariable>(this.Reference);
return this.target;
}
}
Expand Down Expand Up @@ -49,7 +49,7 @@ public override void SerializeV2(IAsyncApiWriter writer)
}
else
{
this.Reference.HostDocument = writer.RootDocument;
this.Reference.Workspace = writer.Workspace;
this.Target.SerializeV2(writer);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Writers/AsyncApiWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public AsyncApiWriterBase(TextWriter textWriter, AsyncApiWriterSettings settings
/// </summary>
protected TextWriter Writer { get; }

public AsyncApiDocument RootDocument { get; set; }
public AsyncApiWorkspace Workspace { get; set; } = new AsyncApiWorkspace();

/// <summary>
/// Write start object.
Expand Down
2 changes: 1 addition & 1 deletion src/LEGO.AsyncAPI/Writers/IAsyncApiWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LEGO.AsyncAPI.Writers
{
public interface IAsyncApiWriter
{
AsyncApiDocument RootDocument { get; set; }
AsyncApiWorkspace Workspace { get; set; }

/// <summary>
/// Write the start object.
Expand Down
2 changes: 0 additions & 2 deletions test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public void Reference()
var reference = doc.Servers.First().Value as AsyncApiServerReference;
reference.Reference.ExternalResource.Should().Be("https://github.com/test/test");
reference.Reference.FragmentId.Should().Be("whatever");
reference.Reference.HostDocument.Should().Be(doc);
reference.Reference.IsFragment.Should().BeTrue();
}

Expand Down Expand Up @@ -136,7 +135,6 @@ public void ServerReference_WithComponentReference_ResolvesReference()
var reference = doc.Servers.First().Value as AsyncApiServerReference;
reference.Reference.ExternalResource.Should().BeNull();
reference.Reference.FragmentId.Should().Be("/components/servers/whatever");
reference.Reference.HostDocument.Should().Be(doc);
reference.Reference.IsFragment.Should().BeTrue();
reference.Url.Should().Be("wss://production.gigantic-server.com:443");

Expand Down

0 comments on commit 06abb47

Please sign in to comment.