Skip to content

Commit

Permalink
Merge pull request #997 from microsoft/vnext
Browse files Browse the repository at this point in the history
Release libs v1.4.0-preview3
  • Loading branch information
MaggieKimani1 authored Sep 2, 2022
2 parents 03ff377 + cf77bb7 commit c382c22
Show file tree
Hide file tree
Showing 26 changed files with 346 additions and 136 deletions.
8 changes: 7 additions & 1 deletion .azure-pipelines/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ stages:
echo "$artifactName"
echo "$artifactVersion"
displayName: 'Fetch Artifact Name'
- task: NuGetCommand@2
displayName: 'NuGet push'
inputs:
Expand All @@ -276,6 +275,7 @@ stages:
publishFeedCredentials: 'OpenAPI Nuget Connection'
- task: GitHubRelease@1
displayName: 'GitHub release (edit)'
condition: succeededOrFailed()
inputs:
gitHubConnection: 'Github-MaggieKimani1'
action: edit
Expand All @@ -285,6 +285,12 @@ stages:
releaseNotesSource: inline
assets: '$(Pipeline.Workspace)\**\*.exe'
changeLogType: issueBased
changeLogLabels: '[
{ "label" : "feature-work", "feature", "displayName" : "New Features", "state" : "closed" },
{ "label" : "enhancement", "V2-Enhancement", "displayName" : "Enhancements", "state" : "closed" },
{ "label" : "bug", "bug-fix", "displayName" : "Bugs", "state" : "closed" },
{ "label" : "documentation", "doc", "displayName" : "Documentation", "state" : "closed"},
{ "label" : "dependencies", "displayName" : "Package Updates", "state" : "closed" }]'

- deployment: deploy_lib
dependsOn: []
Expand Down
24 changes: 24 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the current behavior:

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots/Code Snippets**
If applicable, add screenshots of the stack trace or a code snippet to help explain your problem.
If applicable, add a link to your project

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: ''
labels: ''
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is.

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down
80 changes: 1 addition & 79 deletions src/Microsoft.OpenApi.Hidi/OpenApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,57 +356,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)

return doc;
}

private static async Task<Stream> GetStream(string input, ILogger logger)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

Stream stream;
if (input.StartsWith("http"))
{
try
{
var httpClientHandler = new HttpClientHandler()
{
SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
};
using var httpClient = new HttpClient(httpClientHandler)
{
DefaultRequestVersion = HttpVersion.Version20
};
stream = await httpClient.GetStreamAsync(input);
}
catch (HttpRequestException ex)
{
logger.LogError($"Could not download the file at {input}, reason{ex}");
return null;
}
}
else
{
try
{
var fileInput = new FileInfo(input);
stream = fileInput.OpenRead();
}
catch (Exception ex) when (ex is FileNotFoundException ||
ex is PathTooLongException ||
ex is DirectoryNotFoundException ||
ex is IOException ||
ex is UnauthorizedAccessException ||
ex is SecurityException ||
ex is NotSupportedException)
{
logger.LogError($"Could not open the file at {input}, reason: {ex.Message}");
return null;
}
}
stopwatch.Stop();
logger.LogTrace("{timestamp}ms: Read file {input}", stopwatch.ElapsedMilliseconds, input);
return stream;
}


/// <summary>
/// Takes in a file stream, parses the stream into a JsonDocument and gets a list of paths and Http methods
/// </summary>
Expand Down Expand Up @@ -462,34 +412,6 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
return paths;
}

/// <summary>
/// Fixes the references in the resulting OpenApiDocument.
/// </summary>
/// <param name="document"> The converted OpenApiDocument.</param>
/// <returns> A valid OpenApiDocument instance.</returns>
// private static OpenApiDocument FixReferences2(OpenApiDocument document)
// {
// // This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance.
// // So we write it out, and read it back in again to fix it up.

// OpenApiDocument document;
// logger.LogTrace("Parsing the OpenApi file");
// var result = await new OpenApiStreamReader(new OpenApiReaderSettings
// {
// RuleSet = ValidationRuleSet.GetDefaultRuleSet(),
// BaseUrl = new Uri(openapi)
// }
// ).ReadAsync(stream);

// document = result.OpenApiDocument;
// var context = result.OpenApiDiagnostic;
// var sb = new StringBuilder();
// document.SerializeAsV3(new OpenApiYamlWriter(new StringWriter(sb)));
// var doc = new OpenApiStringReader().Read(sb.ToString(), out _);

// return doc;
// }

/// <summary>
/// Reads stream from file system or makes HTTP request depending on the input string
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.4.0-preview2</Version>
<Version>1.4.0-preview3</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand All @@ -34,7 +34,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SharpYaml" Version="1.9.2" />
<PackageReference Include="SharpYaml" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
{
diagnostic.Warnings.Add(item);
}

}

return document;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal static partial class OpenApiV2Deserializer
"consumes", (o, n) => {
var consumes = n.CreateSimpleList(s => s.GetScalarValue());
if (consumes.Count > 0) {
n.Context.SetTempStorage(TempStorageKeys.OperationConsumes,consumes);
n.Context.SetTempStorage(TempStorageKeys.OperationConsumes,consumes);
}
}
},
Expand Down
24 changes: 0 additions & 24 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiV2VersionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,6 @@ private static ReferenceType ParseReferenceType(string referenceTypeName)
}
}

private static string GetReferenceTypeV2Name(ReferenceType referenceType)
{
switch (referenceType)
{
case ReferenceType.Schema:
return "definitions";

case ReferenceType.Parameter:
return "parameters";

case ReferenceType.Response:
return "responses";

case ReferenceType.Tag:
return "tags";

case ReferenceType.SecurityScheme:
return "securityDefinitions";

default:
throw new ArgumentException();
}
}

private static ReferenceType GetReferenceTypeV2FromName(string referenceType)
{
switch (referenceType)
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.4.0-preview2</Version>
<Version>1.4.0-preview3</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
44 changes: 43 additions & 1 deletion src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Services;
Expand Down Expand Up @@ -62,6 +65,11 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <summary>
/// The unique hash code of the generated OpenAPI document
/// </summary>
public string HashCode => GenerateHashValue(this);

/// <summary>
/// Parameter-less constructor
/// </summary>
Expand Down Expand Up @@ -375,6 +383,40 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
return ResolveReference(reference, false);
}

/// <summary>
/// Takes in an OpenApi document instance and generates its hash value
/// </summary>
/// <param name="doc">The OpenAPI description to hash.</param>
/// <returns>The hash value.</returns>
public static string GenerateHashValue(OpenApiDocument doc)
{
using HashAlgorithm sha = SHA512.Create();
using var cryptoStream = new CryptoStream(Stream.Null, sha, CryptoStreamMode.Write);
using var streamWriter = new StreamWriter(cryptoStream);

var openApiJsonWriter = new OpenApiJsonWriter(streamWriter, new OpenApiJsonWriterSettings { Terse = true });
doc.SerializeAsV3(openApiJsonWriter);
openApiJsonWriter.Flush();

cryptoStream.FlushFinalBlock();
var hash = sha.Hash;

return ConvertByteArrayToString(hash);
}

private static string ConvertByteArrayToString(byte[] hash)
{
// Build the final string by converting each byte
// into hex and appending it to a StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}

return sb.ToString();
}

/// <summary>
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@ public void SerializeAsV2(IOpenApiWriter writer)
{
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
{
var paramName = property.Key;
var paramSchema = property.Value;
if (paramSchema.Type == "string" && paramSchema.Format == "binary") {
if ("string".Equals(paramSchema.Type, StringComparison.OrdinalIgnoreCase)
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
{
paramSchema.Type = "file";
paramSchema.Format = null;
}
Expand Down
12 changes: 8 additions & 4 deletions src/Microsoft.OpenApi/Models/OpenApiSchema.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
Expand Down Expand Up @@ -630,6 +630,10 @@ internal void WriteAsSchemaProperties(
}

// format
Format ??= AllOf?.FirstOrDefault(static x => x.Format != null)?.Format ??
AnyOf?.FirstOrDefault(static x => x.Format != null)?.Format ??
OneOf?.FirstOrDefault(static x => x.Format != null)?.Format;

writer.WriteProperty(OpenApiConstants.Format, Format);

// title
Expand Down Expand Up @@ -695,17 +699,17 @@ internal void WriteAsSchemaProperties(
// allOf
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));

// If there isn't already an AllOf, and the schema contains a oneOf or anyOf write an allOf with the first
// If there isn't already an allOf, and the schema contains a oneOf or anyOf write an allOf with the first
// schema in the list as an attempt to guess at a graceful downgrade situation.
if (AllOf == null || AllOf.Count == 0)
{
// anyOf (Not Supported in V2) - Write the first schema only as an allOf.
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AnyOf.Take(1), (w, s) => s.SerializeAsV2(w));
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AnyOf?.Take(1), (w, s) => s.SerializeAsV2(w));

if (AnyOf == null || AnyOf.Count == 0)
{
// oneOf (Not Supported in V2) - Write the first schema only as an allOf.
writer.WriteOptionalCollection(OpenApiConstants.AllOf, OneOf.Take(1), (w, s) => s.SerializeAsV2(w));
writer.WriteOptionalCollection(OpenApiConstants.AllOf, OneOf?.Take(1), (w, s) => s.SerializeAsV2(w));
}
}

Expand Down
Loading

0 comments on commit c382c22

Please sign in to comment.