Skip to content

Commit ccd4a43

Browse files
Merge pull request #1771 from microsoft/vnext
Deploy libs
2 parents 900d04f + 60258b5 commit ccd4a43

22 files changed

+253
-30
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>latest</LangVersion>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.6.16</Version>
6+
<Version>1.6.17</Version>
77
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
88
<SignAssembly>true</SignAssembly>
99
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.OpenApi.Interfaces
7+
{
8+
/// <summary>
9+
/// Represents an Open API element that can be annotated with
10+
/// non-serializable properties in a property bag.
11+
/// </summary>
12+
public interface IOpenApiAnnotatable
13+
{
14+
/// <summary>
15+
/// A collection of properties associated with the current OpenAPI element.
16+
/// </summary>
17+
IDictionary<string, object> Annotations { get; set; }
18+
}
19+
}

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<LangVersion>Latest</LangVersion>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.6.16</Version>
6+
<Version>1.6.17</Version>
77
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
88
<SignAssembly>true</SignAssembly>
99
<!-- https://github.com/dotnet/sourcelink/blob/main/docs/README.md#embeduntrackedsources -->

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Microsoft.OpenApi.Models
1717
/// <summary>
1818
/// Describes an OpenAPI object (OpenAPI document). See: https://swagger.io/specification
1919
/// </summary>
20-
public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
20+
public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IOpenApiAnnotatable
2121
{
2222
/// <summary>
2323
/// Related workspace containing OpenApiDocuments that are referenced in this document
@@ -70,6 +70,9 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible
7070
/// </summary>
7171
public string HashCode => GenerateHashValue(this);
7272

73+
/// <inheritdoc />
74+
public IDictionary<string, object> Annotations { get; set; }
75+
7376
/// <summary>
7477
/// Parameter-less constructor
7578
/// </summary>
@@ -89,6 +92,7 @@ public OpenApiDocument(OpenApiDocument document)
8992
Tags = document?.Tags != null ? new List<OpenApiTag>(document.Tags) : null;
9093
ExternalDocs = document?.ExternalDocs != null ? new(document?.ExternalDocs) : null;
9194
Extensions = document?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(document.Extensions) : null;
95+
Annotations = document?.Annotations != null ? new Dictionary<string, object>(document.Annotations) : null;
9296
}
9397

9498
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Interfaces;
78
using Microsoft.OpenApi.Writers;
@@ -76,7 +77,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
7677
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, e) => w.WriteAny(e));
7778

7879
// examples
79-
writer.WriteOptionalMap(OpenApiConstants.Examples, Examples, (w, e) => e.SerializeAsV3(w));
80+
SerializeExamples(writer, Examples);
8081

8182
// encoding
8283
writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, (w, e) => e.SerializeAsV3(w));
@@ -94,5 +95,33 @@ public void SerializeAsV2(IOpenApiWriter writer)
9495
{
9596
// Media type does not exist in V2.
9697
}
98+
99+
private static void SerializeExamples(IOpenApiWriter writer, IDictionary<string, OpenApiExample> examples)
100+
{
101+
/* Special case for writing out empty arrays as valid response examples
102+
* Check if there is any example with an empty array as its value and set the flag `hasEmptyArray` to true
103+
* */
104+
var hasEmptyArray = examples.Values.Any( static example =>
105+
example.Value is OpenApiArray arr && arr.Count == 0
106+
);
107+
108+
if (hasEmptyArray)
109+
{
110+
writer.WritePropertyName(OpenApiConstants.Examples);
111+
writer.WriteStartObject();
112+
foreach (var kvp in examples.Where(static kvp => kvp.Value.Value is OpenApiArray arr && arr.Count == 0))
113+
{
114+
writer.WritePropertyName(kvp.Key);
115+
writer.WriteStartObject();
116+
writer.WriteRequiredObject(OpenApiConstants.Value, kvp.Value.Value, (w, v) => w.WriteAny(v));
117+
writer.WriteEndObject();
118+
}
119+
writer.WriteEndObject();
120+
}
121+
else
122+
{
123+
writer.WriteOptionalMap(OpenApiConstants.Examples, examples, (w, e) => e.SerializeAsV3(w));
124+
}
125+
}
97126
}
98127
}

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.OpenApi.Models
1212
/// <summary>
1313
/// Operation Object.
1414
/// </summary>
15-
public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible
15+
public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible, IOpenApiAnnotatable
1616
{
1717
/// <summary>
1818
/// Default value for <see cref="Deprecated"/>.
@@ -105,6 +105,9 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible
105105
/// </summary>
106106
public IDictionary<string, IOpenApiExtension> Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
107107

108+
/// <inheritdoc />
109+
public IDictionary<string, object> Annotations { get; set; }
110+
108111
/// <summary>
109112
/// Parameterless constructor
110113
/// </summary>
@@ -128,6 +131,7 @@ public OpenApiOperation(OpenApiOperation operation)
128131
Security = operation?.Security != null ? new List<OpenApiSecurityRequirement>(operation.Security) : null;
129132
Servers = operation?.Servers != null ? new List<OpenApiServer>(operation.Servers) : null;
130133
Extensions = operation?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(operation.Extensions) : null;
134+
Annotations = operation?.Annotations != null ? new Dictionary<string, object>(operation.Annotations) : null;
131135
}
132136

133137
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiReference.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ public void SerializeAsV3(IOpenApiWriter writer)
148148
return;
149149
}
150150

151-
if (Type == ReferenceType.SecurityScheme)
152-
{
153-
// Write the string as property name
154-
writer.WritePropertyName(ReferenceV3);
155-
return;
156-
}
157-
158151
writer.WriteStartObject();
159152

160153
// $ref

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.OpenApi.Models
1212
/// <summary>
1313
/// Schema Object.
1414
/// </summary>
15-
public class OpenApiSchema : IOpenApiReferenceable, IEffective<OpenApiSchema>, IOpenApiExtensible
15+
public class OpenApiSchema : IOpenApiReferenceable, IEffective<OpenApiSchema>, IOpenApiExtensible, IOpenApiAnnotatable
1616
{
1717
/// <summary>
1818
/// Follow JSON Schema definition. Short text providing information about the data.
@@ -241,6 +241,9 @@ public class OpenApiSchema : IOpenApiReferenceable, IEffective<OpenApiSchema>, I
241241
/// </summary>
242242
public OpenApiReference Reference { get; set; }
243243

244+
/// <inheritdoc />
245+
public IDictionary<string, object> Annotations { get; set; }
246+
244247
/// <summary>
245248
/// Parameterless constructor
246249
/// </summary>
@@ -290,6 +293,7 @@ public OpenApiSchema(OpenApiSchema schema)
290293
Extensions = schema?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null;
291294
UnresolvedReference = schema?.UnresolvedReference ?? UnresolvedReference;
292295
Reference = schema?.Reference != null ? new(schema?.Reference) : null;
296+
Annotations = schema?.Annotations != null ? new Dictionary<string, object>(schema?.Annotations) : null;
293297
}
294298

295299
/// <summary>

src/Microsoft.OpenApi/Models/OpenApiSecurityRequirement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
5050
continue;
5151
}
5252

53-
securityScheme.SerializeAsV3(writer);
53+
writer.WritePropertyName(securityScheme.Reference.ReferenceV3);
5454

5555
writer.WriteStartArray();
5656

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiMediaTypeTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using System.IO;
55
using FluentAssertions;
66
using Microsoft.OpenApi.Any;
7+
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Readers.ParseNodes;
910
using Microsoft.OpenApi.Readers.V3;
11+
using Microsoft.OpenApi.Tests;
1012
using Xunit;
1113

1214
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
@@ -77,5 +79,43 @@ public void ParseMediaTypeWithExamplesShouldSucceed()
7779
}
7880
});
7981
}
82+
83+
[Fact]
84+
public void ParseMediaTypeWithEmptyArrayInExamplesWorks()
85+
{
86+
// Arrange
87+
var expected = @"{
88+
""schema"": {
89+
""type"": ""array"",
90+
""items"": {
91+
""type"": ""object"",
92+
""properties"": {
93+
""id"": {
94+
""type"": ""string""
95+
}
96+
}
97+
}
98+
},
99+
""examples"": {
100+
""Success response - no results"": {
101+
""value"": [ ]
102+
}
103+
}
104+
}
105+
";
106+
MapNode node;
107+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "examplesWithEmptyArray.json")))
108+
{
109+
node = TestHelper.CreateYamlMapNode(stream);
110+
}
111+
112+
// Act
113+
var mediaType = OpenApiV3Deserializer.LoadMediaType(node);
114+
var serialized = mediaType.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
115+
116+
// Assert
117+
serialized.MakeLineBreaksEnvironmentNeutral()
118+
.Should().BeEquivalentTo(expected.MakeLineBreaksEnvironmentNeutral());
119+
}
80120
}
81121
}

0 commit comments

Comments
 (0)