Skip to content

Commit c382c22

Browse files
Merge pull request #997 from microsoft/vnext
Release libs v1.4.0-preview3
2 parents 03ff377 + cf77bb7 commit c382c22

26 files changed

+346
-136
lines changed

.azure-pipelines/ci-build.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ stages:
266266
echo "$artifactName"
267267
echo "$artifactVersion"
268268
displayName: 'Fetch Artifact Name'
269-
270269
- task: NuGetCommand@2
271270
displayName: 'NuGet push'
272271
inputs:
@@ -276,6 +275,7 @@ stages:
276275
publishFeedCredentials: 'OpenAPI Nuget Connection'
277276
- task: GitHubRelease@1
278277
displayName: 'GitHub release (edit)'
278+
condition: succeededOrFailed()
279279
inputs:
280280
gitHubConnection: 'Github-MaggieKimani1'
281281
action: edit
@@ -285,6 +285,12 @@ stages:
285285
releaseNotesSource: inline
286286
assets: '$(Pipeline.Workspace)\**\*.exe'
287287
changeLogType: issueBased
288+
changeLogLabels: '[
289+
{ "label" : "feature-work", "feature", "displayName" : "New Features", "state" : "closed" },
290+
{ "label" : "enhancement", "V2-Enhancement", "displayName" : "Enhancements", "state" : "closed" },
291+
{ "label" : "bug", "bug-fix", "displayName" : "Bugs", "state" : "closed" },
292+
{ "label" : "documentation", "doc", "displayName" : "Documentation", "state" : "closed"},
293+
{ "label" : "dependencies", "displayName" : "Package Updates", "state" : "closed" }]'
288294

289295
- deployment: deploy_lib
290296
dependsOn: []

.github/ISSUE_TEMPLATE/bug_report.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the current behavior:
15+
16+
**Expected behavior**
17+
A clear and concise description of what you expected to happen.
18+
19+
**Screenshots/Code Snippets**
20+
If applicable, add screenshots of the stack trace or a code snippet to help explain your problem.
21+
If applicable, add a link to your project
22+
23+
**Additional context**
24+
Add any other context about the problem here.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is.
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

Microsoft.OpenApi.Hidi.Tests/Microsoft.OpenApi.Hidi.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
1313
<PackageReference Include="Moq" Version="4.18.2" />
1414
<PackageReference Include="xunit" Version="2.4.2" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

+1-79
Original file line numberDiff line numberDiff line change
@@ -356,57 +356,7 @@ public static OpenApiDocument FixReferences(OpenApiDocument document)
356356

357357
return doc;
358358
}
359-
360-
private static async Task<Stream> GetStream(string input, ILogger logger)
361-
{
362-
var stopwatch = new Stopwatch();
363-
stopwatch.Start();
364-
365-
Stream stream;
366-
if (input.StartsWith("http"))
367-
{
368-
try
369-
{
370-
var httpClientHandler = new HttpClientHandler()
371-
{
372-
SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
373-
};
374-
using var httpClient = new HttpClient(httpClientHandler)
375-
{
376-
DefaultRequestVersion = HttpVersion.Version20
377-
};
378-
stream = await httpClient.GetStreamAsync(input);
379-
}
380-
catch (HttpRequestException ex)
381-
{
382-
logger.LogError($"Could not download the file at {input}, reason{ex}");
383-
return null;
384-
}
385-
}
386-
else
387-
{
388-
try
389-
{
390-
var fileInput = new FileInfo(input);
391-
stream = fileInput.OpenRead();
392-
}
393-
catch (Exception ex) when (ex is FileNotFoundException ||
394-
ex is PathTooLongException ||
395-
ex is DirectoryNotFoundException ||
396-
ex is IOException ||
397-
ex is UnauthorizedAccessException ||
398-
ex is SecurityException ||
399-
ex is NotSupportedException)
400-
{
401-
logger.LogError($"Could not open the file at {input}, reason: {ex.Message}");
402-
return null;
403-
}
404-
}
405-
stopwatch.Stop();
406-
logger.LogTrace("{timestamp}ms: Read file {input}", stopwatch.ElapsedMilliseconds, input);
407-
return stream;
408-
}
409-
359+
410360
/// <summary>
411361
/// Takes in a file stream, parses the stream into a JsonDocument and gets a list of paths and Http methods
412362
/// </summary>
@@ -462,34 +412,6 @@ private static Dictionary<string, List<string>> EnumerateJsonDocument(JsonElemen
462412
return paths;
463413
}
464414

465-
/// <summary>
466-
/// Fixes the references in the resulting OpenApiDocument.
467-
/// </summary>
468-
/// <param name="document"> The converted OpenApiDocument.</param>
469-
/// <returns> A valid OpenApiDocument instance.</returns>
470-
// private static OpenApiDocument FixReferences2(OpenApiDocument document)
471-
// {
472-
// // This method is only needed because the output of ConvertToOpenApi isn't quite a valid OpenApiDocument instance.
473-
// // So we write it out, and read it back in again to fix it up.
474-
475-
// OpenApiDocument document;
476-
// logger.LogTrace("Parsing the OpenApi file");
477-
// var result = await new OpenApiStreamReader(new OpenApiReaderSettings
478-
// {
479-
// RuleSet = ValidationRuleSet.GetDefaultRuleSet(),
480-
// BaseUrl = new Uri(openapi)
481-
// }
482-
// ).ReadAsync(stream);
483-
484-
// document = result.OpenApiDocument;
485-
// var context = result.OpenApiDiagnostic;
486-
// var sb = new StringBuilder();
487-
// document.SerializeAsV3(new OpenApiYamlWriter(new StringWriter(sb)));
488-
// var doc = new OpenApiStringReader().Read(sb.ToString(), out _);
489-
490-
// return doc;
491-
// }
492-
493415
/// <summary>
494416
/// Reads stream from file system or makes HTTP request depending on the input string
495417
/// </summary>

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.4.0-preview2</Version>
13+
<Version>1.4.0-preview3</Version>
1414
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>
@@ -34,7 +34,7 @@
3434
</PropertyGroup>
3535

3636
<ItemGroup>
37-
<PackageReference Include="SharpYaml" Version="1.9.2" />
37+
<PackageReference Include="SharpYaml" Version="2.1.0" />
3838
</ItemGroup>
3939

4040
<ItemGroup>

src/Microsoft.OpenApi.Readers/OpenApiYamlDocumentReader.cs

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public OpenApiDocument Read(YamlDocument input, out OpenApiDiagnostic diagnostic
7979
{
8080
diagnostic.Warnings.Add(item);
8181
}
82-
8382
}
8483

8584
return document;

src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal static partial class OpenApiV2Deserializer
6060
"consumes", (o, n) => {
6161
var consumes = n.CreateSimpleList(s => s.GetScalarValue());
6262
if (consumes.Count > 0) {
63-
n.Context.SetTempStorage(TempStorageKeys.OperationConsumes,consumes);
63+
n.Context.SetTempStorage(TempStorageKeys.OperationConsumes,consumes);
6464
}
6565
}
6666
},

src/Microsoft.OpenApi.Readers/V2/OpenApiV2VersionService.cs

-24
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,6 @@ private static ReferenceType ParseReferenceType(string referenceTypeName)
107107
}
108108
}
109109

110-
private static string GetReferenceTypeV2Name(ReferenceType referenceType)
111-
{
112-
switch (referenceType)
113-
{
114-
case ReferenceType.Schema:
115-
return "definitions";
116-
117-
case ReferenceType.Parameter:
118-
return "parameters";
119-
120-
case ReferenceType.Response:
121-
return "responses";
122-
123-
case ReferenceType.Tag:
124-
return "tags";
125-
126-
case ReferenceType.SecurityScheme:
127-
return "securityDefinitions";
128-
129-
default:
130-
throw new ArgumentException();
131-
}
132-
}
133-
134110
private static ReferenceType GetReferenceTypeV2FromName(string referenceType)
135111
{
136112
switch (referenceType)

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<Company>Microsoft</Company>
1212
<Title>Microsoft.OpenApi</Title>
1313
<PackageId>Microsoft.OpenApi</PackageId>
14-
<Version>1.4.0-preview2</Version>
14+
<Version>1.4.0-preview3</Version>
1515
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
1616
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1717
<PackageTags>OpenAPI .NET</PackageTags>

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
67
using System.Linq;
8+
using System.Security.Cryptography;
9+
using System.Text;
710
using Microsoft.OpenApi.Exceptions;
811
using Microsoft.OpenApi.Interfaces;
912
using Microsoft.OpenApi.Services;
@@ -62,6 +65,11 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
6265
/// </summary>
6366
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
6467

68+
/// <summary>
69+
/// The unique hash code of the generated OpenAPI document
70+
/// </summary>
71+
public string HashCode => GenerateHashValue(this);
72+
6573
/// <summary>
6674
/// Parameter-less constructor
6775
/// </summary>
@@ -375,6 +383,40 @@ public IOpenApiReferenceable ResolveReference(OpenApiReference reference)
375383
return ResolveReference(reference, false);
376384
}
377385

386+
/// <summary>
387+
/// Takes in an OpenApi document instance and generates its hash value
388+
/// </summary>
389+
/// <param name="doc">The OpenAPI description to hash.</param>
390+
/// <returns>The hash value.</returns>
391+
public static string GenerateHashValue(OpenApiDocument doc)
392+
{
393+
using HashAlgorithm sha = SHA512.Create();
394+
using var cryptoStream = new CryptoStream(Stream.Null, sha, CryptoStreamMode.Write);
395+
using var streamWriter = new StreamWriter(cryptoStream);
396+
397+
var openApiJsonWriter = new OpenApiJsonWriter(streamWriter, new OpenApiJsonWriterSettings { Terse = true });
398+
doc.SerializeAsV3(openApiJsonWriter);
399+
openApiJsonWriter.Flush();
400+
401+
cryptoStream.FlushFinalBlock();
402+
var hash = sha.Hash;
403+
404+
return ConvertByteArrayToString(hash);
405+
}
406+
407+
private static string ConvertByteArrayToString(byte[] hash)
408+
{
409+
// Build the final string by converting each byte
410+
// into hex and appending it to a StringBuilder
411+
StringBuilder sb = new StringBuilder();
412+
for (int i = 0; i < hash.Length; i++)
413+
{
414+
sb.Append(hash[i].ToString("X2"));
415+
}
416+
417+
return sb.ToString();
418+
}
419+
378420
/// <summary>
379421
/// Load the referenced <see cref="IOpenApiReferenceable"/> object from a <see cref="OpenApiReference"/> object
380422
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,11 @@ public void SerializeAsV2(IOpenApiWriter writer)
253253
{
254254
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
255255
{
256-
var paramName = property.Key;
257256
var paramSchema = property.Value;
258-
if (paramSchema.Type == "string" && paramSchema.Format == "binary") {
257+
if ("string".Equals(paramSchema.Type, StringComparison.OrdinalIgnoreCase)
258+
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
259+
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
260+
{
259261
paramSchema.Type = "file";
260262
paramSchema.Format = null;
261263
}

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
@@ -630,6 +630,10 @@ internal void WriteAsSchemaProperties(
630630
}
631631

632632
// format
633+
Format ??= AllOf?.FirstOrDefault(static x => x.Format != null)?.Format ??
634+
AnyOf?.FirstOrDefault(static x => x.Format != null)?.Format ??
635+
OneOf?.FirstOrDefault(static x => x.Format != null)?.Format;
636+
633637
writer.WriteProperty(OpenApiConstants.Format, Format);
634638

635639
// title
@@ -695,17 +699,17 @@ internal void WriteAsSchemaProperties(
695699
// allOf
696700
writer.WriteOptionalCollection(OpenApiConstants.AllOf, AllOf, (w, s) => s.SerializeAsV2(w));
697701

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

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

0 commit comments

Comments
 (0)