Skip to content

Commit

Permalink
fix: change payload 'any' type to 'schema' to match specification. (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean authored Nov 27, 2022
1 parent cbf3253 commit 3a7b96a
Show file tree
Hide file tree
Showing 40 changed files with 2,215 additions and 822 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) The LEGO Group. All rights reserved.

namespace LEGO.AsyncAPI.Readers.V2
namespace LEGO.AsyncAPI.Readers
{
using System;
using System.Collections.Generic;
using System.IO;
using LEGO.AsyncAPI.Models.Interfaces;
using LEGO.AsyncAPI.Readers;
using LEGO.AsyncAPI.Validations;

public enum ReferenceResolutionSetting
Expand Down
10 changes: 4 additions & 6 deletions src/LEGO.AsyncAPI.Readers/AsyncApiStreamReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) The LEGO Group. All rights reserved.

using LEGO.AsyncAPI.Readers.V2;

namespace LEGO.AsyncAPI.Readers
{
using System.IO;
Expand Down Expand Up @@ -35,8 +33,8 @@ public AsyncApiStreamReader(AsyncApiReaderSettings settings = null)
public AsyncApiDocument Read(Stream input, out AsyncApiDiagnostic diagnostic)
{
var reader = new StreamReader(input);
var result = new AsyncApiTextReaderReader(settings).Read(reader, out diagnostic);
if (!settings.LeaveStreamOpen)
var result = new AsyncApiTextReaderReader(this.settings).Read(reader, out diagnostic);
if (!this.settings.LeaveStreamOpen)
{
reader.Dispose();
}
Expand Down Expand Up @@ -67,7 +65,7 @@ public async Task<ReadResult> ReadAsync(Stream input)

var reader = new StreamReader(bufferedStream);

return await new AsyncApiTextReaderReader(settings).ReadAsync(reader);
return await new AsyncApiTextReaderReader(this.settings).ReadAsync(reader);
}

/// <summary>
Expand All @@ -82,7 +80,7 @@ public T ReadFragment<T>(Stream input, AsyncApiVersion version, out AsyncApiDiag
{
using (var reader = new StreamReader(input))
{
return new AsyncApiTextReaderReader(settings).ReadFragment<T>(reader, version, out diagnostic);
return new AsyncApiTextReaderReader(this.settings).ReadFragment<T>(reader, version, out diagnostic);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/LEGO.AsyncAPI.Readers/AsyncApiStringReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) The LEGO Group. All rights reserved.

using LEGO.AsyncAPI.Readers.V2;

namespace LEGO.AsyncAPI.Readers
{
using System.IO;
Expand Down Expand Up @@ -32,7 +30,7 @@ public AsyncApiDocument Read(string input, out AsyncApiDiagnostic diagnostic)
{
using (var reader = new StringReader(input))
{
return new AsyncApiTextReaderReader(settings).Read(reader, out diagnostic);
return new AsyncApiTextReaderReader(this.settings).Read(reader, out diagnostic);
}
}

Expand All @@ -44,7 +42,7 @@ public T ReadFragment<T>(string input, AsyncApiVersion version, out AsyncApiDiag
{
using (var reader = new StringReader(input))
{
return new AsyncApiTextReaderReader(settings).ReadFragment<T>(reader, version, out diagnostic);
return new AsyncApiTextReaderReader(this.settings).ReadFragment<T>(reader, version, out diagnostic);
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/LEGO.AsyncAPI.Readers/AsyncApiTextReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) The LEGO Group. All rights reserved.

using LEGO.AsyncAPI.Readers.V2;

namespace LEGO.AsyncAPI.Readers
{
using System.IO;
Expand Down Expand Up @@ -51,7 +49,7 @@ public AsyncApiDocument Read(TextReader input, out AsyncApiDiagnostic diagnostic
return new AsyncApiDocument();
}

return new AsyncApiYamlDocumentReader(settings).Read(yamlDocument, out diagnostic);
return new AsyncApiYamlDocumentReader(this.settings).Read(yamlDocument, out diagnostic);
}

/// <summary>
Expand Down Expand Up @@ -79,7 +77,7 @@ public async Task<ReadResult> ReadAsync(TextReader input)
};
}

return await new AsyncApiYamlDocumentReader(settings).ReadAsync(yamlDocument);
return await new AsyncApiYamlDocumentReader(this.settings).ReadAsync(yamlDocument);
}

/// <summary>
Expand All @@ -106,7 +104,7 @@ public T ReadFragment<T>(TextReader input, AsyncApiVersion version, out AsyncApi
return default;
}

return new AsyncApiYamlDocumentReader(settings).ReadFragment<T>(yamlDocument, version,
return new AsyncApiYamlDocumentReader(this.settings).ReadFragment<T>(yamlDocument, version,
out diagnostic);
}

Expand Down
26 changes: 12 additions & 14 deletions src/LEGO.AsyncAPI.Readers/AsyncApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) The LEGO Group. All rights reserved.

using LEGO.AsyncAPI.Readers.V2;

namespace LEGO.AsyncAPI.Readers
{
using System.Collections.Generic;
Expand Down Expand Up @@ -42,24 +40,24 @@ public AsyncApiDocument Read(YamlDocument input, out AsyncApiDiagnostic diagnost
diagnostic = new AsyncApiDiagnostic();
var context = new ParsingContext(diagnostic)
{
ExtensionParsers = settings.ExtensionParsers,
ExtensionParsers = this.settings.ExtensionParsers,
};

AsyncApiDocument document = null;
try
{
document = context.Parse(input);

ResolveReferences(diagnostic, document);
this.ResolveReferences(diagnostic, document);
}
catch (AsyncApiException ex)
{
diagnostic.Errors.Add(new AsyncApiError(ex));
}

if (settings.RuleSet != null && settings.RuleSet.Rules.Count > 0)
if (this.settings.RuleSet != null && this.settings.RuleSet.Rules.Count > 0)
{
var asyncApiErrors = document.Validate(settings.RuleSet);
var asyncApiErrors = document.Validate(this.settings.RuleSet);
foreach (var item in asyncApiErrors.Where(e => e is AsyncApiValidatorError))
{
diagnostic.Errors.Add(item);
Expand All @@ -79,25 +77,25 @@ public async Task<ReadResult> ReadAsync(YamlDocument input)
var diagnostic = new AsyncApiDiagnostic();
var context = new ParsingContext(diagnostic)
{
ExtensionParsers = settings.ExtensionParsers,
ExtensionParsers = this.settings.ExtensionParsers,
};

AsyncApiDocument document = null;
try
{
// Parse the AsyncApi Document
document = context.Parse(input);
ResolveReferences(diagnostic, document);
this.ResolveReferences(diagnostic, document);
}
catch (AsyncApiException ex)
{
diagnostic.Errors.Add(new AsyncApiError(ex));
}

// Validate the document
if (settings.RuleSet != null && settings.RuleSet.Rules.Count > 0)
if (this.settings.RuleSet != null && this.settings.RuleSet.Rules.Count > 0)
{
var errors = document.Validate(settings.RuleSet);
var errors = document.Validate(this.settings.RuleSet);
foreach (var item in errors)
{
diagnostic.Errors.Add(item);
Expand All @@ -116,7 +114,7 @@ private void ResolveReferences(AsyncApiDiagnostic diagnostic, AsyncApiDocument d
var errors = new List<AsyncApiError>();

// Resolve References if requested
switch (settings.ReferenceResolution)
switch (this.settings.ReferenceResolution)
{
case ReferenceResolutionSetting.ResolveReferences:
errors.AddRange(document.ResolveReferences());
Expand Down Expand Up @@ -145,7 +143,7 @@ public T ReadFragment<T>(YamlDocument input, AsyncApiVersion version, out AsyncA
diagnostic = new AsyncApiDiagnostic();
var context = new ParsingContext(diagnostic)
{
ExtensionParsers = settings.ExtensionParsers,
ExtensionParsers = this.settings.ExtensionParsers,
};

IAsyncApiElement element = null;
Expand All @@ -160,9 +158,9 @@ public T ReadFragment<T>(YamlDocument input, AsyncApiVersion version, out AsyncA
}

// Validate the element
if (settings.RuleSet != null && settings.RuleSet.Rules.Count > 0)
if (this.settings.RuleSet != null && this.settings.RuleSet.Rules.Count > 0)
{
var errors = element.Validate(settings.RuleSet);
var errors = element.Validate(this.settings.RuleSet);
foreach (var item in errors)
{
diagnostic.Errors.Add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ internal static partial class AsyncApiV2Deserializer
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
{ "topic", (a, n) => { a.Topic = n.GetScalarValue(); } },
{ "partitions", (a, n) => { a.Partitions = n.GetIntegerValue(); } },
{ "topicConfiguration", (a, n) => { a.TopicConfiguration = LoadTopicConfiguration(n); } },
{ "replicas", (a, n) => { a.Replicas = n.GetIntegerValue(); } },
};

private static FixedFieldMap<TopicConfigurationObject> kafkaChannelTopicConfigurationObjectFixedFields = new()
{
{ "cleanup.policy", (a, n) => { a.CleanupPolicy = n.CreateSimpleList(s => s.GetScalarValue()); } },
{ "retention.ms", (a, n) => { a.RetentionMiliseconds = n.GetIntegerValue(); } },
{ "retention.bytes", (a, n) => { a.RetentionBytes = n.GetIntegerValue(); } },
{ "delete.retention.ms", (a, n) => { a.DeleteRetentionMiliseconds = n.GetIntegerValue(); } },
{ "max.message.bytes", (a, n) => { a.MaxMessageBytes = n.GetIntegerValue(); } },
};

private static FixedFieldMap<KafkaOperationBinding> kafkaOperationBindingFixedFields = new()
{
{ "bindingVersion", (a, n) => { a.BindingVersion = n.GetScalarValue(); } },
Expand All @@ -37,5 +47,13 @@ internal static partial class AsyncApiV2Deserializer
{ "schemaIdPayloadEncoding", (a, n) => { a.SchemaIdPayloadEncoding = n.GetScalarValue(); } },
{ "schemaLookupStrategy", (a, n) => { a.SchemaLookupStrategy = n.GetScalarValue(); } },
};

private static TopicConfigurationObject LoadTopicConfiguration(ParseNode node)
{
var mapNode = node.CheckMapNode("topicConfiguration");
var retention = new TopicConfigurationObject();
ParseMap(mapNode, retention, kafkaChannelTopicConfigurationObjectFixedFields, null);
return retention;
}
}
}
6 changes: 3 additions & 3 deletions src/LEGO.AsyncAPI.Readers/ParseNodes/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override int GetIntegerValue()
return value;
}

throw new AsyncApiReaderException("Value could not parse to integer", node);
throw new AsyncApiReaderException("Value could not parse to integer", this.node);
}

public override long GetLongValue()
Expand All @@ -46,7 +46,7 @@ public override long GetLongValue()
return value;
}

throw new AsyncApiReaderException("Value could not parse to long", node);
throw new AsyncApiReaderException("Value could not parse to long", this.node);
}

public override bool GetBooleanValue()
Expand All @@ -56,7 +56,7 @@ public override bool GetBooleanValue()
return value;
}

throw new AsyncApiReaderException("Value could not parse to bool", node);
throw new AsyncApiReaderException("Value could not parse to bool", this.node);
}

public override IAsyncApiAny CreateAny()
Expand Down
73 changes: 0 additions & 73 deletions src/LEGO.AsyncAPI.Readers/ParsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ namespace LEGO.AsyncAPI.Readers
public class ParsingContext
{
private readonly Stack<string> currentLocation = new ();
private readonly Dictionary<string, object> tempStorage = new ();
private readonly Dictionary<object, Dictionary<string, object>> scopedTempStorage = new ();
private readonly Dictionary<string, Stack<string>> loopStacks = new ();

internal Dictionary<string, Func<IAsyncApiAny, IAsyncApiExtension>> ExtensionParsers
{
Expand Down Expand Up @@ -104,79 +101,9 @@ public string GetLocation()
this.currentLocation.Reverse().Select(s => s.Replace("~", "~0").Replace("/", "~1")).ToArray());
}

public T GetFromTempStorage<T>(string key, object scope = null)
{
Dictionary<string, object> storage;

if (scope == null)
{
storage = this.tempStorage;
}
else if (!this.scopedTempStorage.TryGetValue(scope, out storage))
{
return default(T);
}

return storage.TryGetValue(key, out var value) ? (T)value : default(T);
}

public void SetTempStorage(string key, object value, object scope = null)
{
Dictionary<string, object> storage;

if (scope == null)
{
storage = this.tempStorage;
}
else if (!this.scopedTempStorage.TryGetValue(scope, out storage))
{
storage = this.scopedTempStorage[scope] = new Dictionary<string, object>();
}

if (value == null)
{
storage.Remove(key);
}
else
{
storage[key] = value;
}
}

public void StartObject(string objectName)
{
this.currentLocation.Push(objectName);
}

public bool PushLoop(string loopId, string key)
{
Stack<string> stack;
if (!this.loopStacks.TryGetValue(loopId, out stack))
{
stack = new Stack<string>();
this.loopStacks.Add(loopId, stack);
}

if (!stack.Contains(key))
{
stack.Push(key);
return true;
}

return false; // Loop detected
}

internal void ClearLoop(string loopid)
{
this.loopStacks[loopid].Clear();
}

public void PopLoop(string loopid)
{
if (this.loopStacks[loopid].Count > 0)
{
this.loopStacks[loopid].Pop();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static partial class AsyncApiV2Deserializer
{
private static FixedFieldMap<AsyncApiDocument> asyncApiFixedFields = new()
{
{ "asyncapi", (a, n) => { } },
{ "asyncapi", (a, n) => { a.Asyncapi = "2.5.0"; } },
{ "id", (a, n) => a.Id = n.GetScalarValue() },
{ "info", (a, n) => a.Info = LoadInfo(n) },
{ "servers", (a, n) => a.Servers = n.CreateMap(LoadServer) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static partial class AsyncApiV2Deserializer
"headers", (a, n) => { a.Headers = LoadSchema(n); }
},
{
"payload", (a, n) => { a.Payload = n.CreateAny(); }
"payload", (a, n) => { a.Payload = LoadSchema(n); }
},
{
"correlationId", (a, n) => { a.CorrelationId = LoadCorrelationId(n); }
Expand Down
Loading

0 comments on commit 3a7b96a

Please sign in to comment.