Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Collections.Generic;
using Microsoft.OpenApi.Readers.Interface;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
public class OpenApiDiagnostic : IDiagnostic
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
public class OpenApiError
{
Expand Down
91 changes: 91 additions & 0 deletions src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System.IO;
using System.Linq;
using Microsoft.OpenApi.Readers.Interface;
using Microsoft.OpenApi.Readers.ParseNodes;
using SharpYaml;
using SharpYaml.Serialization;
using Microsoft.OpenApi.Models;

namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Service class for converting streams into OpenApiDocument instances
/// </summary>
public class OpenApiStreamReader : IOpenApiReader<Stream, OpenApiDiagnostic>
{
/// <summary>
/// Gets the version of the Open API document.
/// </summary>
private static string GetVersion(RootNode rootNode)
{
var versionNode = rootNode.Find(new JsonPointer("/openapi"));

if (versionNode != null)
{
return versionNode.GetScalarValue();
}

versionNode = rootNode.Find(new JsonPointer("/swagger"));

return versionNode?.GetScalarValue();
}

/// <summary>
/// Reads the stream input and parses it into an Open API document.
/// </summary>
public OpenApiDocument Read(Stream input, out OpenApiDiagnostic diagnostic)
{
RootNode rootNode;
var context = new ParsingContext();
diagnostic = new OpenApiDiagnostic();

try
{
using (var streamReader = new StreamReader(input))
{
var yamlStream = new YamlStream();
yamlStream.Load(streamReader);

var yamlDocument = yamlStream.Documents.First();
rootNode = new RootNode(context, diagnostic, yamlDocument);
}
}
catch (SyntaxErrorException ex)
{
diagnostic.Errors.Add(new OpenApiError(string.Empty, ex.Message));

return new OpenApiDocument();
}

var inputVersion = GetVersion(rootNode);

switch (inputVersion)
{
case "2.0":
context.SetReferenceService(
new OpenApiReferenceService(rootNode)
{
loadReference = OpenApiV2Deserializer.LoadReference,
parseReference = p => OpenApiV2Deserializer.ParseReference(p)
});

return OpenApiV2Deserializer.LoadOpenApi(rootNode);

default:
context.SetReferenceService(
new OpenApiReferenceService(rootNode)
{
loadReference = OpenApiV3Deserializer.LoadReference,
parseReference = p => new OpenApiReference(p)
});

return OpenApiV3Deserializer.LoadOpenApi(rootNode);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.OpenApi.Readers.Interface;
using Microsoft.OpenApi.Models;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Service class for converting strings into OpenApiDocument instances
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System;
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Readers.YamlReaders.ParseNodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Class containing logic to deserialize Open API V2 document into
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using System;
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Readers.YamlReaders.ParseNodes;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
/// <summary>
/// Class containing logic to deserialize Open API V3 document into
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using System.Collections.Generic;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal class FixedFieldMap<T> : Dictionary<string, Action<T, ParseNode>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using SharpYaml.Serialization;
using Microsoft.OpenApi.Any;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal class ListNode : ParseNode, IEnumerable<ParseNode>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
/// <summary>
/// Abstraction of a Map to isolate semantic parsing from details of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal abstract class ParseNode
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using System.Collections.Generic;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal class PatternFieldMap<T> : Dictionary<Func<string, bool>, Action<T, string, ParseNode>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using SharpYaml.Serialization;
using Microsoft.OpenApi.Any;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal class PropertyNode : ParseNode
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System;
using SharpYaml.Serialization;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
/// <summary>
/// Wrapper class around YamlDocument to isolate semantic parsing from details of Yaml DOM.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using SharpYaml.Serialization;
using Microsoft.OpenApi.Any;

namespace Microsoft.OpenApi.Readers.YamlReaders.ParseNodes
namespace Microsoft.OpenApi.Readers.ParseNodes
{
internal class ValueNode : ParseNode
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
public class ParsingContext
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
internal class OpenApiReferenceService : IOpenApiReferenceService
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Linq;
using SharpYaml.Serialization;

namespace Microsoft.OpenApi.Readers.YamlReaders
namespace Microsoft.OpenApi.Readers
{
internal static class YamlHelper
{
Expand Down
91 changes: 0 additions & 91 deletions src/Microsoft.OpenApi.Readers/YamlReaders/OpenApiStreamReader.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Workbench/MainModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Text;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.YamlReaders;
using Microsoft.OpenApi.Readers;

namespace Microsoft.OpenApi.Workbench
{
Expand Down
1 change: 0 additions & 1 deletion test/Microsoft.OpenApi.Readers.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.OpenApi.Readers.YamlReaders;
using SharpYaml.Serialization;
using Xunit;

Expand Down
1 change: 0 additions & 1 deletion test/Microsoft.OpenApi.Readers.Tests/CallbackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// ------------------------------------------------------------

using System.Linq;
using Microsoft.OpenApi.Readers.YamlReaders;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests
Expand Down
3 changes: 1 addition & 2 deletions test/Microsoft.OpenApi.Readers.Tests/FixtureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

using System.IO;
using System.Linq;
using Microsoft.OpenApi.Readers.YamlReaders;
using Microsoft.OpenApi.Readers.YamlReaders.ParseNodes;
using Microsoft.OpenApi.Readers.ParseNodes;
using SharpYaml.Serialization;
using Xunit;

Expand Down
1 change: 0 additions & 1 deletion test/Microsoft.OpenApi.Readers.Tests/InfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// ------------------------------------------------------------

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Readers.YamlReaders;
using System.Linq;
using Xunit;

Expand Down
Loading