diff --git a/src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs b/src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs index 73bdafc6..83d69ab6 100644 --- a/src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs +++ b/src/LEGO.AsyncAPI.Readers/AsyncApiJsonDocumentReader.cs @@ -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); @@ -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); diff --git a/src/LEGO.AsyncAPI.Readers/AsyncApiReferenceHostDocumentResolver.cs b/src/LEGO.AsyncAPI.Readers/AsyncApiReferenceHostDocumentResolver.cs index c170207b..50bad0a3 100644 --- a/src/LEGO.AsyncAPI.Readers/AsyncApiReferenceHostDocumentResolver.cs +++ b/src/LEGO.AsyncAPI.Readers/AsyncApiReferenceHostDocumentResolver.cs @@ -9,13 +9,13 @@ namespace LEGO.AsyncAPI.Readers internal class AsyncApiReferenceHostDocumentResolver : AsyncApiVisitorBase { - private AsyncApiDocument currentDocument; + private AsyncApiWorkspace workspace; private List errors = new List(); public AsyncApiReferenceHostDocumentResolver( - AsyncApiDocument currentDocument) + AsyncApiWorkspace workspace) { - this.currentDocument = currentDocument; + this.workspace = workspace; } public IEnumerable Errors @@ -30,7 +30,7 @@ public override void Visit(IAsyncApiReferenceable referenceable) { if (referenceable.Reference != null) { - referenceable.Reference.HostDocument = this.currentDocument; + referenceable.Reference.Workspace = this.workspace; } } } diff --git a/src/LEGO.AsyncAPI.Readers/Services/AsyncApiRemoteReferenceCollector.cs b/src/LEGO.AsyncAPI.Readers/Services/AsyncApiRemoteReferenceCollector.cs index 1d33251e..79e732a1 100644 --- a/src/LEGO.AsyncAPI.Readers/Services/AsyncApiRemoteReferenceCollector.cs +++ b/src/LEGO.AsyncAPI.Readers/Services/AsyncApiRemoteReferenceCollector.cs @@ -10,12 +10,12 @@ namespace LEGO.AsyncAPI.Readers.Services internal class AsyncApiRemoteReferenceCollector : AsyncApiVisitorBase { private readonly List references = new(); - private AsyncApiDocument currentDocument; + private AsyncApiWorkspace workspace; public AsyncApiRemoteReferenceCollector( - AsyncApiDocument currentDocument) + AsyncApiWorkspace workspace) { - this.currentDocument = currentDocument; + this.workspace = workspace; } /// /// List of all external references collected from AsyncApiDocument. @@ -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); diff --git a/src/LEGO.AsyncAPI/AsyncApiWorkspace.cs b/src/LEGO.AsyncAPI/AsyncApiWorkspace.cs index 9bcf8eeb..bb352ea8 100644 --- a/src/LEGO.AsyncAPI/AsyncApiWorkspace.cs +++ b/src/LEGO.AsyncAPI/AsyncApiWorkspace.cs @@ -161,6 +161,12 @@ public bool Contains(string location) return this.resolvedReferenceRegistry.ContainsKey(key) || this.artifactsRegistry.ContainsKey(key); } + public T ResolveReference(AsyncApiReference reference) + where T : class + { + return this.ResolveReference(reference.Reference); + } + public T ResolveReference(string location) where T : class { diff --git a/src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs b/src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs index 5aad6e40..adc797ef 100644 --- a/src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs +++ b/src/LEGO.AsyncAPI/Models/AsyncApiDocument.cs @@ -74,8 +74,8 @@ public void SerializeV2(IAsyncApiWriter writer) } this.Workspace.RegisterComponents(this); + writer.Workspace = this.Workspace; - writer.RootDocument = this; writer.WriteStartObject(); // asyncApi diff --git a/src/LEGO.AsyncAPI/Models/AsyncApiReference.cs b/src/LEGO.AsyncAPI/Models/AsyncApiReference.cs index 69e819ee..8964e1d3 100644 --- a/src/LEGO.AsyncAPI/Models/AsyncApiReference.cs +++ b/src/LEGO.AsyncAPI/Models/AsyncApiReference.cs @@ -105,7 +105,7 @@ public string ExternalResource /// /// Gets or sets the AsyncApiDocument that is hosting the AsyncApiReference instance. This is used to enable dereferencing the reference. /// - public AsyncApiDocument HostDocument { get; set; } = null; + public AsyncApiWorkspace Workspace { get; set; } = null; /// /// Gets a flag indicating whether a file is a valid OpenAPI document or a fragment. diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiChannelReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiChannelReference.cs index 504dbf00..437c4e50 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiChannelReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiChannelReference.cs @@ -14,7 +14,7 @@ private AsyncApiChannel Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiCorrelationIdReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiCorrelationIdReference.cs index e423d8e3..89784131 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiCorrelationIdReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiCorrelationIdReference.cs @@ -17,7 +17,7 @@ private AsyncApiCorrelationId Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiJsonSchemaReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiJsonSchemaReference.cs index a3b7d729..a7211559 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiJsonSchemaReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiJsonSchemaReference.cs @@ -15,7 +15,7 @@ private AsyncApiJsonSchema Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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)) { diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageReference.cs index 2e6dbdf2..05aa8ed8 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageReference.cs @@ -17,7 +17,7 @@ private AsyncApiMessage Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageTraitReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageTraitReference.cs index 76cc38db..118ad4b9 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageTraitReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiMessageTraitReference.cs @@ -17,7 +17,7 @@ private AsyncApiMessageTrait Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiOperationTraitReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiOperationTraitReference.cs index 92bee6fe..36bb3bd9 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiOperationTraitReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiOperationTraitReference.cs @@ -17,7 +17,7 @@ private AsyncApiOperationTrait Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiParameterReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiParameterReference.cs index 8ae13fc3..1c570ca0 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiParameterReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiParameterReference.cs @@ -17,7 +17,7 @@ private AsyncApiParameter Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiSecuritySchemeReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiSecuritySchemeReference.cs index 75b8a189..7e8440ef 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiSecuritySchemeReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiSecuritySchemeReference.cs @@ -18,7 +18,7 @@ private AsyncApiSecurityScheme Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiServerReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiServerReference.cs index 4927b3f2..1ef0d510 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiServerReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiServerReference.cs @@ -17,7 +17,7 @@ private AsyncApiServer Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Models/References/AsyncApiServerVariableReference.cs b/src/LEGO.AsyncAPI/Models/References/AsyncApiServerVariableReference.cs index 82cb6ae0..c5bb968d 100644 --- a/src/LEGO.AsyncAPI/Models/References/AsyncApiServerVariableReference.cs +++ b/src/LEGO.AsyncAPI/Models/References/AsyncApiServerVariableReference.cs @@ -17,7 +17,7 @@ private AsyncApiServerVariable Target { get { - this.target ??= this.Reference.HostDocument?.ResolveReference(this.Reference); + this.target ??= this.Reference.Workspace?.ResolveReference(this.Reference); return this.target; } } @@ -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); } } diff --git a/src/LEGO.AsyncAPI/Writers/AsyncApiWriterBase.cs b/src/LEGO.AsyncAPI/Writers/AsyncApiWriterBase.cs index aa123d7b..d6d6b750 100644 --- a/src/LEGO.AsyncAPI/Writers/AsyncApiWriterBase.cs +++ b/src/LEGO.AsyncAPI/Writers/AsyncApiWriterBase.cs @@ -68,7 +68,7 @@ public AsyncApiWriterBase(TextWriter textWriter, AsyncApiWriterSettings settings /// protected TextWriter Writer { get; } - public AsyncApiDocument RootDocument { get; set; } + public AsyncApiWorkspace Workspace { get; set; } = new AsyncApiWorkspace(); /// /// Write start object. diff --git a/src/LEGO.AsyncAPI/Writers/IAsyncApiWriter.cs b/src/LEGO.AsyncAPI/Writers/IAsyncApiWriter.cs index 7116a7c5..e85bb834 100644 --- a/src/LEGO.AsyncAPI/Writers/IAsyncApiWriter.cs +++ b/src/LEGO.AsyncAPI/Writers/IAsyncApiWriter.cs @@ -6,7 +6,7 @@ namespace LEGO.AsyncAPI.Writers { public interface IAsyncApiWriter { - AsyncApiDocument RootDocument { get; set; } + AsyncApiWorkspace Workspace { get; set; } /// /// Write the start object. diff --git a/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs b/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs index 44c37574..abfcef91 100644 --- a/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs +++ b/test/LEGO.AsyncAPI.Tests/Models/AsyncApiReference_Should.cs @@ -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(); } @@ -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");