Skip to content

Commit f0f0a05

Browse files
committed
Merge remote-tracking branch 'origin/vnext' into mk/fix-tags-serialization
2 parents c1d831f + e6ab1cd commit f0f0a05

File tree

24 files changed

+152
-88
lines changed

24 files changed

+152
-88
lines changed

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<PrivateAssets>all</PrivateAssets>
3939
</PackageReference>
4040
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
41-
<PackageReference Include="Microsoft.OData.Edm" Version="8.2.1" />
41+
<PackageReference Include="Microsoft.OData.Edm" Version="8.2.2" />
4242
<PackageReference Include="Microsoft.OpenApi.OData" Version="2.0.0-preview.7" />
4343
<PackageReference Include="Microsoft.OpenApi.ApiManifest" Version="0.5.0-preview" />
4444
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
</PackageReference>
3232

3333
<PackageReference Include="SharpYaml" Version="2.1.1" />
34-
<PackageReference Include="System.Text.Json" Version="9.0.0" />
34+
<PackageReference Include="System.Text.Json" Version="[8.0,)" />
35+
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-hh2w-p6rv-4g7w" />
36+
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-8g4q-xg66-9fp4" />
3537
</ItemGroup>
3638

3739
<ItemGroup>

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MainModel : INotifyPropertyChanged
4949
/// </summary>
5050
private OpenApiSpecVersion _version = OpenApiSpecVersion.OpenApi3_0;
5151

52-
private HttpClient _httpClient = new();
52+
private static readonly HttpClient _httpClient = new();
5353

5454
public string Input
5555
{
@@ -166,31 +166,31 @@ public OpenApiSpecVersion Version
166166
public bool IsYaml
167167
{
168168
get => Format == OpenApiFormat.Yaml;
169-
set => Format = OpenApiFormat.Yaml;
169+
set => Format = value ? OpenApiFormat.Yaml : Format;
170170
}
171171

172172
public bool IsJson
173173
{
174174
get => Format == OpenApiFormat.Json;
175-
set => Format = OpenApiFormat.Json;
175+
set => Format = value ? OpenApiFormat.Json : Format;
176176
}
177177

178178
public bool IsV2_0
179179
{
180180
get => Version == OpenApiSpecVersion.OpenApi2_0;
181-
set => Version = OpenApiSpecVersion.OpenApi2_0;
181+
set => Version = value ? OpenApiSpecVersion.OpenApi2_0 : Version;
182182
}
183183

184184
public bool IsV3_0
185185
{
186186
get => Version == OpenApiSpecVersion.OpenApi3_0;
187-
set => Version = OpenApiSpecVersion.OpenApi3_0;
187+
set => Version = value ? OpenApiSpecVersion.OpenApi3_0 : Version;
188188
}
189189

190190
public bool IsV3_1
191191
{
192192
get => Version == OpenApiSpecVersion.OpenApi3_1;
193-
set => Version = OpenApiSpecVersion.OpenApi3_1;
193+
set => Version = value ? OpenApiSpecVersion.OpenApi3_1 : Version;
194194
}
195195

196196
/// <summary>
@@ -219,14 +219,8 @@ internal async Task ParseDocumentAsync()
219219
{
220220
if (!string.IsNullOrWhiteSpace(_inputFile))
221221
{
222-
if (_inputFile.StartsWith("http"))
223-
{
224-
stream = await _httpClient.GetStreamAsync(_inputFile);
225-
}
226-
else
227-
{
228-
stream = new FileStream(_inputFile, FileMode.Open);
229-
}
222+
stream = _inputFile.StartsWith("http") ? await _httpClient.GetStreamAsync(_inputFile)
223+
: new FileStream(_inputFile, FileMode.Open);
230224
}
231225
else
232226
{
@@ -245,20 +239,13 @@ internal async Task ParseDocumentAsync()
245239
ReferenceResolution = ResolveExternal ? ReferenceResolutionSetting.ResolveAllReferences : ReferenceResolutionSetting.ResolveLocalReferences,
246240
RuleSet = ValidationRuleSet.GetDefaultRuleSet()
247241
};
248-
if (ResolveExternal)
242+
if (ResolveExternal && !string.IsNullOrWhiteSpace(_inputFile))
249243
{
250-
if (_inputFile.StartsWith("http"))
251-
{
252-
settings.BaseUrl = new(_inputFile);
253-
}
254-
else
255-
{
256-
settings.BaseUrl = new("file://" + Path.GetDirectoryName(_inputFile) + "/");
257-
}
244+
settings.BaseUrl = _inputFile.StartsWith("http") ? new(_inputFile)
245+
: new("file://" + Path.GetDirectoryName(_inputFile) + "/");
258246
}
259247

260-
var format = OpenApiModelFactory.GetFormat(_inputFile);
261-
var readResult = await OpenApiDocument.LoadAsync(stream, format);
248+
var readResult = await OpenApiDocument.LoadAsync(stream, Format.GetDisplayName());
262249
var document = readResult.OpenApiDocument;
263250
var context = readResult.OpenApiDiagnostic;
264251

@@ -306,7 +293,6 @@ internal async Task ParseDocumentAsync()
306293
stream.Close();
307294
await stream.DisposeAsync();
308295
}
309-
310296
}
311297
}
312298

@@ -332,7 +318,7 @@ private string WriteContents(OpenApiDocument document)
332318
return new StreamReader(outputStream).ReadToEnd();
333319
}
334320

335-
private MemoryStream CreateStream(string text)
321+
private static MemoryStream CreateStream(string text)
336322
{
337323
var stream = new MemoryStream();
338324
var writer = new StreamWriter(stream);

src/Microsoft.OpenApi.Workbench/Microsoft.OpenApi.Workbench.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<UseWPF>true</UseWPF>
77
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
88
<EnableWindowsTargeting>true</EnableWindowsTargeting>
9+
<NoWarn>NU1903</NoWarn>
910
</PropertyGroup>
1011
<ItemGroup>
1112
<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.12.19">

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
2323
</PropertyGroup>
2424
<ItemGroup>
25-
<PackageReference Include="System.Text.Json" Version="9.0.0" />
25+
<PackageReference Include="System.Text.Json" Version="[8.0,)" />
26+
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-hh2w-p6rv-4g7w" />
27+
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-8g4q-xg66-9fp4" />
2628
</ItemGroup>
2729

2830
<ItemGroup>

src/Microsoft.OpenApi/Models/OpenApiConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public static class OpenApiConstants
5050
/// </summary>
5151
public const string Title = "title";
5252

53+
/// <summary>
54+
/// Field: Const
55+
/// </summary>
56+
public const string Const = "const";
57+
5358
/// <summary>
5459
/// Field: Type
5560
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public class OpenApiSchema : IOpenApiAnnotatable, IOpenApiExtensible, IOpenApiRe
8383
/// </summary>
8484
public virtual JsonSchemaType? Type { get; set; }
8585

86+
/// <summary>
87+
/// Follow JSON Schema definition: https://json-schema.org/draft/2020-12/json-schema-validation
88+
/// </summary>
89+
public virtual string Const { get; set; }
90+
8691
/// <summary>
8792
/// Follow JSON Schema definition: https://tools.ietf.org/html/draft-fge-json-schema-validation-00
8893
/// While relying on JSON Schema's defined formats,
@@ -347,6 +352,7 @@ public OpenApiSchema(OpenApiSchema schema)
347352
{
348353
Title = schema?.Title ?? Title;
349354
Id = schema?.Id ?? Id;
355+
Const = schema?.Const ?? Const;
350356
Schema = schema?.Schema ?? Schema;
351357
Comment = schema?.Comment ?? Comment;
352358
Vocabulary = schema?.Vocabulary != null ? new Dictionary<string, bool>(schema.Vocabulary) : null;
@@ -563,6 +569,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
563569
writer.WriteProperty(OpenApiConstants.Id, Id);
564570
writer.WriteProperty(OpenApiConstants.DollarSchema, Schema);
565571
writer.WriteProperty(OpenApiConstants.Comment, Comment);
572+
writer.WriteProperty(OpenApiConstants.Const, Const);
566573
writer.WriteOptionalMap(OpenApiConstants.Vocabulary, Vocabulary, (w, s) => w.WriteValue(s));
567574
writer.WriteOptionalMap(OpenApiConstants.Defs, Definitions, (w, s) => s.SerializeAsV31(w));
568575
writer.WriteProperty(OpenApiConstants.DynamicRef, DynamicRef);

src/Microsoft.OpenApi/Models/References/OpenApiSchemaReference.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class OpenApiSchemaReference : OpenApiSchema
1717
internal OpenApiSchema _target;
1818
private readonly OpenApiReference _reference;
1919
private string _description;
20+
private JsonNode _default;
21+
private JsonNode _example;
22+
private IList<JsonNode> _examples;
2023

2124
private OpenApiSchema Target
2225
{
@@ -90,6 +93,8 @@ internal OpenApiSchemaReference(OpenApiSchema target, string referenceId)
9093
/// <inheritdoc/>
9194
public override JsonSchemaType? Type { get => Target.Type; set => Target.Type = value; }
9295
/// <inheritdoc/>
96+
public override string Const { get => Target.Const; set => Target.Const = value; }
97+
/// <inheritdoc/>
9398
public override string Format { get => Target.Format; set => Target.Format = value; }
9499
/// <inheritdoc/>
95100
public override string Description
@@ -114,7 +119,11 @@ public override string Description
114119
/// <inheritdoc/>
115120
public override decimal? MultipleOf { get => Target.MultipleOf; set => Target.MultipleOf = value; }
116121
/// <inheritdoc/>
117-
public override JsonNode Default { get => Target.Default; set => Target.Default = value; }
122+
public override JsonNode Default
123+
{
124+
get => _default ??= Target.Default;
125+
set => _default = value;
126+
}
118127
/// <inheritdoc/>
119128
public override bool ReadOnly { get => Target.ReadOnly; set => Target.ReadOnly = value; }
120129
/// <inheritdoc/>
@@ -152,9 +161,17 @@ public override string Description
152161
/// <inheritdoc/>
153162
public override OpenApiDiscriminator Discriminator { get => Target.Discriminator; set => Target.Discriminator = value; }
154163
/// <inheritdoc/>
155-
public override JsonNode Example { get => Target.Example; set => Target.Example = value; }
164+
public override JsonNode Example
165+
{
166+
get => _example ??= Target.Example;
167+
set => _example = value;
168+
}
156169
/// <inheritdoc/>
157-
public override IList<JsonNode> Examples { get => Target.Examples; set => Target.Examples = value; }
170+
public override IList<JsonNode> Examples
171+
{
172+
get => _examples ??= Target.Examples;
173+
set => Target.Examples = value;
174+
}
158175
/// <inheritdoc/>
159176
public override IList<JsonNode> Enum { get => Target.Enum; set => Target.Enum = value; }
160177
/// <inheritdoc/>

src/Microsoft.OpenApi/Reader/ParsingContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ public void PopLoop(string loopid)
271271

272272
private void ValidateRequiredFields(OpenApiDocument doc, string version)
273273
{
274-
if ((version.is2_0() || version.is3_0()) && (doc.Paths == null || !doc.Paths.Any()))
274+
if ((version.is2_0() || version.is3_0()) && (doc.Paths == null))
275275
{
276-
// paths is a required field in OpenAPI 3.0 but optional in 3.1
276+
// paths is a required field in OpenAPI 2.0 and 3.0 but optional in 3.1
277277
RootNode.Context.Diagnostic.Errors.Add(new OpenApiError("", $"Paths is a REQUIRED field at {RootNode.Context.GetLocation()}"));
278278
}
279279
}

src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ internal static partial class OpenApiV31Deserializer
131131
}
132132
}
133133
},
134+
{
135+
"const",
136+
(o, n, _) => o.Const = n.GetScalarValue()
137+
},
134138
{
135139
"allOf",
136140
(o, n, t) => o.AllOf = n.CreateList(LoadSchema, t)

0 commit comments

Comments
 (0)