Skip to content

Commit

Permalink
Merge pull request #1278 from microsoft/mk/integrate-json-schema-library
Browse files Browse the repository at this point in the history
JsonSchema.NET Integration
  • Loading branch information
MaggieKimani1 authored Nov 21, 2023
2 parents 159138c + 1afe195 commit 8058d24
Show file tree
Hide file tree
Showing 281 changed files with 8,552 additions and 7,392 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ jobs:
name: Build
runs-on: windows-latest
steps:
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'adopt'
java-version: 11
java-version: 17
- name: Setup .NET 5 # At the moment the scanner requires dotnet 5 https://www.nuget.org/packages/dotnet-sonarscanner
uses: actions/setup-dotnet@v3
with:
Expand Down
74 changes: 37 additions & 37 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
using System.IO;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Xsl;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.OData.Edm.Csdl;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
Expand All @@ -21,11 +26,6 @@
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Writers;
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
using System.Threading;
using System.Xml.Xsl;
using System.Xml;
using System.Reflection;
using Microsoft.Extensions.Configuration;

namespace Microsoft.OpenApi.Hidi
{
Expand Down Expand Up @@ -98,7 +98,7 @@ CancellationToken cancellationToken
}
}

private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineLocal, bool inlineExternal, OpenApiFormat openApiFormat, OpenApiSpecVersion openApiVersion, OpenApiDocument document, ILogger logger)
private static void WriteOpenApi(FileInfo output, bool terseOutput, bool inlineLocal, bool inlineExternal, OpenApiFormat openApiFormat, OpenApiSpecVersion openApiVersion, OpenApiDocument document, ILogger logger)
{
using (logger.BeginScope("Output"))
{
Expand Down Expand Up @@ -135,7 +135,7 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
{
OpenApiDocument document;
Stream stream;

if (!string.IsNullOrEmpty(csdl))
{
var stopwatch = new Stopwatch();
Expand Down Expand Up @@ -168,7 +168,7 @@ private static async Task<OpenApiDocument> GetOpenApi(string openapi, string csd
return document;
}

private static async Task<OpenApiDocument> FilterOpenApiDocument(string filterbyoperationids, string filterbytags, string filterbycollection, OpenApiDocument document, ILogger logger, CancellationToken cancellationToken)
private static async Task<OpenApiDocument> FilterOpenApiDocument(string filterbyoperationids, string filterbytags, string filterbycollection, OpenApiDocument document, ILogger logger, CancellationToken cancellationToken)
{
using (logger.BeginScope("Filter"))
{
Expand Down Expand Up @@ -239,8 +239,8 @@ private static Stream ApplyFilterToCsdl(Stream csdlStream, string entitySetOrSin
/// Implementation of the validate command
/// </summary>
public static async Task ValidateOpenApiDocument(
string openapi,
ILogger logger,
string openapi,
ILogger logger,
CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(openapi))
Expand Down Expand Up @@ -285,7 +285,7 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
result = await new OpenApiStreamReader(new OpenApiReaderSettings
{
LoadExternalRefs = inlineExternal,
BaseUrl = openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ?
BaseUrl = openApiFile.StartsWith("http", StringComparison.OrdinalIgnoreCase) ?
new Uri(openApiFile) :
new Uri("file://" + new FileInfo(openApiFile).DirectoryName + Path.DirectorySeparatorChar)
}
Expand All @@ -296,7 +296,7 @@ private static async Task<ReadResult> ParseOpenApi(string openApiFile, bool inli
LogErrors(logger, result);
stopwatch.Stop();
}

return result;
}

Expand All @@ -310,7 +310,7 @@ internal static IConfiguration GetConfiguration(string settingsFile)

return config;
}

/// <summary>
/// Converts CSDL to OpenAPI
/// </summary>
Expand All @@ -329,7 +329,7 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl, stri
{
settings.SemVerVersion = metadataVersion;
}

config.GetSection("OpenApiConvertSettings").Bind(settings);

OpenApiDocument document = edmModel.ConvertToOpenApi(settings);
Expand All @@ -354,7 +354,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)

return doc;
}

/// <summary>
/// Takes in a file stream, parses the stream into a JsonDocument and gets a list of paths and Http methods
/// </summary>
Expand All @@ -377,13 +377,13 @@ public static Dictionary<string, List<string>> ParseJsonCollectionFile(Stream st
private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElement itemElement, Dictionary<string, List<string>> paths)
{
var itemsArray = itemElement.GetProperty("item");

foreach (var item in itemsArray.EnumerateArray())
{
if(item.ValueKind == JsonValueKind.Object)
if (item.ValueKind == JsonValueKind.Object)
{
if(item.TryGetProperty("request", out var request))
{
if (item.TryGetProperty("request", out var request))
{
// Fetch list of methods and urls from collection, store them in a dictionary
var path = request.GetProperty("url").GetProperty("raw").ToString();
var method = request.GetProperty("method").ToString();
Expand All @@ -395,11 +395,11 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
{
paths[path].Add(method);
}
}
else
{
}
else
{
EnumerateJsonDocument(item, paths);
}
}
}
else
{
Expand Down Expand Up @@ -508,11 +508,11 @@ internal static async Task<string> ShowOpenApiDocument(string openapi, string cs
if (output == null)
{
var tempPath = Path.GetTempPath() + "/hidi/";
if(!File.Exists(tempPath))
if (!File.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
}

var fileName = Path.GetRandomFileName();

output = new FileInfo(Path.Combine(tempPath, fileName + ".html"));
Expand All @@ -528,7 +528,7 @@ internal static async Task<string> ShowOpenApiDocument(string openapi, string cs
process.StartInfo.FileName = output.FullName;
process.StartInfo.UseShellExecute = true;
process.Start();

return output.FullName;
}
else // Write diagram as Markdown document to output file
Expand All @@ -540,7 +540,7 @@ internal static async Task<string> ShowOpenApiDocument(string openapi, string cs
}
logger.LogTrace("Created markdown document with diagram ");
return output.FullName;
}
}
}
}
catch (TaskCanceledException)
Expand All @@ -563,7 +563,7 @@ private static void LogErrors(ILogger logger, ReadResult result)
{
foreach (var error in context.Errors)
{
logger.LogError($"Detected error during parsing: {error}",error.ToString());
logger.LogError($"Detected error during parsing: {error}", error.ToString());
}
}
}
Expand All @@ -581,7 +581,7 @@ internal static void WriteTreeDocumentAsMarkdown(string openapiUrl, OpenApiDocum
// write a span for each mermaidcolorscheme
foreach (var style in OpenApiUrlTreeNode.MermaidNodeStyles)
{
writer.WriteLine($"<span style=\"padding:2px;background-color:{style.Value.Color};border: 2px solid\">{style.Key.Replace("_"," ")}</span>");
writer.WriteLine($"<span style=\"padding:2px;background-color:{style.Value.Color};border: 2px solid\">{style.Key.Replace("_", " ")}</span>");
}
writer.WriteLine("</div>");
writer.WriteLine();
Expand Down Expand Up @@ -609,7 +609,7 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
writer.WriteLine("<h1>" + document.Info.Title + "</h1>");
writer.WriteLine();
writer.WriteLine($"<h3> API Description: <a href='{sourceUrl}'>{sourceUrl}</a></h3>");

writer.WriteLine(@"<div>");
// write a span for each mermaidcolorscheme
foreach (var style in OpenApiUrlTreeNode.MermaidNodeStyles)
Expand All @@ -622,8 +622,8 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
rootNode.WriteMermaid(writer);
writer.WriteLine("</code>");

// Write script tag to include JS library for rendering markdown
writer.WriteLine(@"<script>
// Write script tag to include JS library for rendering markdown
writer.WriteLine(@"<script>
var config = {
startOnLoad:true,
theme: 'forest',
Expand All @@ -635,8 +635,8 @@ internal static void WriteTreeDocumentAsHtml(string sourceUrl, OpenApiDocument d
mermaid.initialize(config);
window.mermaid.init(undefined, document.querySelectorAll('.language-mermaid'));
</script>");
// Write script tag to include JS library for rendering mermaid
writer.WriteLine("</html");
// Write script tag to include JS library for rendering mermaid
writer.WriteLine("</html");
}
}
}
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi.Hidi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Hidi.Handlers;
Expand Down Expand Up @@ -51,7 +48,7 @@ internal static RootCommand CreateRootCommand()

var formatOption = new Option<OpenApiFormat?>("--format", "File format");
formatOption.AddAlias("-f");

var terseOutputOption = new Option<bool>("--terse-output", "Produce terse json output");
terseOutputOption.AddAlias("--to");

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.OpenApi.Hidi/StatsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using Json.Schema;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;

Expand All @@ -19,7 +20,7 @@ public override void Visit(OpenApiParameter parameter)

public int SchemaCount { get; set; } = 0;

public override void Visit(OpenApiSchema schema)
public override void Visit(ref JsonSchema schema)
{
SchemaCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Text.Json.Nodes;
using Microsoft.OpenApi.Exceptions;
using SharpYaml.Serialization;

namespace Microsoft.OpenApi.Readers.Exceptions
{
Expand All @@ -30,7 +29,8 @@ public OpenApiReaderException(string message) : base(message) { }
/// </summary>
/// <param name="message">Plain text error message for this exception.</param>
/// <param name="context">Context of current parsing process.</param>
public OpenApiReaderException(string message, ParsingContext context) : base(message) {
public OpenApiReaderException(string message, ParsingContext context) : base(message)
{
Pointer = context.GetLocation();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<LangVersion>9.0</LangVersion>
Expand Down Expand Up @@ -35,7 +35,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonSchema.Net" Version="4.1.5" />
<PackageReference Include="JsonSchema.Net.OpenApi" Version="1.1.0" />
<PackageReference Include="SharpYaml" Version="2.1.0" />
<PackageReference Include="Yaml2JsonNode" Version="1.1.1" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

Expand Down
7 changes: 3 additions & 4 deletions src/Microsoft.OpenApi.Readers/OpenApiReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Readers.Interface;
using Microsoft.OpenApi.Validations;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Nodes;

namespace Microsoft.OpenApi.Readers
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public OpenApiStreamReader(OpenApiReaderSettings settings = null)
{
_settings = settings ?? new OpenApiReaderSettings();

if((_settings.ReferenceResolution == ReferenceResolutionSetting.ResolveAllReferences || _settings.LoadExternalRefs)
if ((_settings.ReferenceResolution == ReferenceResolutionSetting.ResolveAllReferences || _settings.LoadExternalRefs)
&& _settings.BaseUrl == null)
{
throw new ArgumentException("BaseUrl must be provided to resolve external references.");
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static bool is2_0(this string version)
{
result = true;
}

return result;
}

Expand Down
5 changes: 1 addition & 4 deletions src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,7 +15,6 @@
using Microsoft.OpenApi.Readers.Services;
using Microsoft.OpenApi.Services;
using Microsoft.OpenApi.Validations;
using SharpYaml.Serialization;

namespace Microsoft.OpenApi.Readers
{
Expand Down Expand Up @@ -71,7 +68,7 @@ public OpenApiDocument Read(JsonNode input, out OpenApiDiagnostic diagnostic)
}

// Validate the document
if (_settings.RuleSet != null && _settings.RuleSet.Rules.Count() > 0)
if (_settings.RuleSet != null && _settings.RuleSet.Rules.Any())
{
var openApiErrors = document.Validate(_settings.RuleSet);
foreach (var item in openApiErrors.OfType<OpenApiValidatorError>())
Expand Down
Loading

0 comments on commit 8058d24

Please sign in to comment.