Skip to content

Commit 6a830b8

Browse files
committed
Merge remote-tracking branch 'origin/vnext' into mk/fix-empty-paths-failing
2 parents 1af4f13 + 98b01e0 commit 6a830b8

File tree

10 files changed

+118
-30
lines changed

10 files changed

+118
-30
lines changed

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/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/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)

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,5 +452,48 @@ public void SerializeSchemaWithJsonSchemaKeywordsWorks()
452452
schema.Vocabulary.Keys.Count.Should().Be(5);
453453
schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
454454
}
455+
456+
[Fact]
457+
public void ParseSchemaWithConstWorks()
458+
{
459+
var expected = @"{
460+
""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
461+
""required"": [
462+
""status""
463+
],
464+
""type"": ""object"",
465+
""properties"": {
466+
""status"": {
467+
""const"": ""active"",
468+
""type"": ""string""
469+
},
470+
""user"": {
471+
""required"": [
472+
""role""
473+
],
474+
""type"": ""object"",
475+
""properties"": {
476+
""role"": {
477+
""const"": ""admin"",
478+
""type"": ""string""
479+
}
480+
}
481+
}
482+
}
483+
}";
484+
485+
var path = Path.Combine(SampleFolderPath, "schemaWithConst.json");
486+
487+
// Act
488+
var schema = OpenApiModelFactory.Load<OpenApiSchema>(path, OpenApiSpecVersion.OpenApi3_1, out _);
489+
schema.Properties["status"].Const.Should().Be("active");
490+
schema.Properties["user"].Properties["role"].Const.Should().Be("admin");
491+
492+
// serialization
493+
var writer = new StringWriter();
494+
schema.SerializeAsV31(new OpenApiJsonWriter(writer));
495+
var schemaString = writer.ToString();
496+
schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
497+
}
455498
}
456499
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"type": "object",
4+
"properties": {
5+
"status": {
6+
"type": "string",
7+
"const": "active"
8+
},
9+
"user": {
10+
"type": "object",
11+
"properties": {
12+
"role": {
13+
"type": "string",
14+
"const": "admin"
15+
}
16+
},
17+
"required": [ "role" ]
18+
}
19+
},
20+
"required": [ "status" ]
21+
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ namespace Microsoft.OpenApi.Models
398398
public const string Comment = "$comment";
399399
public const string Components = "components";
400400
public const string ComponentsSegment = "/components/";
401+
public const string Const = "const";
401402
public const string Consumes = "consumes";
402403
public const string Contact = "contact";
403404
public const string Content = "content";
@@ -882,6 +883,7 @@ namespace Microsoft.OpenApi.Models
882883
public virtual System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AllOf { get; set; }
883884
public virtual System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AnyOf { get; set; }
884885
public virtual string Comment { get; set; }
886+
public virtual string Const { get; set; }
885887
public virtual System.Text.Json.Nodes.JsonNode Default { get; set; }
886888
public virtual System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiSchema> Definitions { get; set; }
887889
public virtual bool Deprecated { get; set; }
@@ -1222,6 +1224,7 @@ namespace Microsoft.OpenApi.Models.References
12221224
public override System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AllOf { get; set; }
12231225
public override System.Collections.Generic.IList<Microsoft.OpenApi.Models.OpenApiSchema> AnyOf { get; set; }
12241226
public override string Comment { get; set; }
1227+
public override string Const { get; set; }
12251228
public override System.Text.Json.Nodes.JsonNode Default { get; set; }
12261229
public override System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiSchema> Definitions { get; set; }
12271230
public override bool Deprecated { get; set; }

test/Microsoft.OpenApi.Trimming.Tests/Microsoft.OpenApi.Trimming.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<PublishAot>true</PublishAot>
88
<TrimmerSingleWarn>false</TrimmerSingleWarn>
99
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
10+
<NoWarn>NU1903</NoWarn>
1011
<IsPackable>false</IsPackable>
1112
</PropertyGroup>
1213

0 commit comments

Comments
 (0)