Skip to content

Commit 7f754b7

Browse files
committed
Code refactoring; replace JsonSchema with OpenApiSchema
1 parent 8e020d8 commit 7f754b7

34 files changed

+413
-1487
lines changed

src/Microsoft.OpenApi.Hidi/StatsVisitor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using Json.Schema;
76
using Microsoft.OpenApi.Models;
87
using Microsoft.OpenApi.Services;
98

@@ -20,7 +19,7 @@ public override void Visit(OpenApiParameter parameter)
2019

2120
public int SchemaCount { get; set; }
2221

23-
public override void Visit(ref JsonSchema schema)
22+
public override void Visit(OpenApiSchema schema)
2423
{
2524
SchemaCount++;
2625
}

src/Microsoft.OpenApi.Workbench/MainModel.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.OpenApi.Extensions;
1212
using Microsoft.OpenApi.Models;
1313
using Microsoft.OpenApi.Reader;
14-
using Microsoft.OpenApi.Readers;
1514
using Microsoft.OpenApi.Services;
1615
using Microsoft.OpenApi.Validations;
1716

src/Microsoft.OpenApi/Helpers/JsonNodeCloneHelper.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Text.Json;
55
using System.Text.Json.Nodes;
66
using System.Text.Json.Serialization;
7-
using Json.Schema;
87
using Microsoft.OpenApi.Any;
98

109
namespace Microsoft.OpenApi.Helpers
@@ -28,18 +27,6 @@ internal static OpenApiAny Clone(OpenApiAny value)
2827
return new OpenApiAny(result);
2928
}
3029

31-
internal static JsonSchema CloneJsonSchema(JsonSchema schema)
32-
{
33-
var jsonString = Serialize(schema);
34-
if (string.IsNullOrEmpty(jsonString))
35-
{
36-
return null;
37-
}
38-
39-
var result = JsonSerializer.Deserialize<JsonSchema>(jsonString, options);
40-
return result;
41-
}
42-
4330
private static string Serialize(object obj)
4431
{
4532
if (obj == null)

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
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;
66
using System.Linq;
7-
using Json.Schema;
87
using Microsoft.OpenApi.Any;
98
using Microsoft.OpenApi.Interfaces;
109
using Microsoft.OpenApi.Writers;
@@ -165,7 +164,7 @@ internal OpenApiBodyParameter ConvertToBodyParameter()
165164
// V2 spec actually allows the body to have custom name.
166165
// To allow round-tripping we use an extension to hold the name
167166
Name = "body",
168-
Schema = Content.Values.FirstOrDefault()?.Schema ?? new JsonSchemaBuilder(),
167+
Schema = Content.Values.FirstOrDefault()?.Schema ?? new OpenApiSchema(),
169168
Examples = Content.Values.FirstOrDefault()?.Examples,
170169
Required = Required,
171170
Extensions = Extensions.ToDictionary(static k => k.Key, static v => v.Value) // Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
@@ -184,24 +183,23 @@ internal IEnumerable<OpenApiFormDataParameter> ConvertToFormDataParameters()
184183
if (Content == null || !Content.Any())
185184
yield break;
186185

187-
foreach (var property in Content.First().Value.Schema.GetProperties())
186+
foreach (var property in Content.First().Value.Schema.Properties)
188187
{
189188
var paramSchema = property.Value;
190-
if (paramSchema.GetType().Equals(SchemaValueType.String)
191-
&& ("binary".Equals(paramSchema.GetFormat().Key, StringComparison.OrdinalIgnoreCase)
192-
|| "base64".Equals(paramSchema.GetFormat().Key, StringComparison.OrdinalIgnoreCase)))
189+
if ("string".Equals(paramSchema.Type.ToString(), StringComparison.OrdinalIgnoreCase)
190+
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
191+
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
193192
{
194-
// JsonSchema is immutable so these can't be set
195-
//paramSchema.Type("file");
196-
//paramSchema.Format(null);
193+
paramSchema.Type = "file";
194+
paramSchema.Format = null;
197195
}
198196
yield return new()
199197
{
200-
Description = property.Value.GetDescription(),
198+
Description = property.Value.Description,
201199
Name = property.Key,
202200
Schema = property.Value,
203201
Examples = Content.Values.FirstOrDefault()?.Examples,
204-
Required = Content.First().Value.Schema.GetRequired().Contains(property.Key)
202+
Required = Content.First().Value.Schema.Required?.Contains(property.Key) ?? false
205203
};
206204
}
207205
}

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 7 additions & 7 deletions
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;
@@ -715,6 +715,12 @@ internal void WriteAsSchemaProperties(
715715
ISet<string> parentRequiredProperties,
716716
string propertyName)
717717
{
718+
// type
719+
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
720+
721+
// description
722+
writer.WriteProperty(OpenApiConstants.Description, Description);
723+
718724
// format
719725
if (string.IsNullOrEmpty(Format))
720726
{
@@ -728,9 +734,6 @@ internal void WriteAsSchemaProperties(
728734
// title
729735
writer.WriteProperty(OpenApiConstants.Title, Title);
730736

731-
// description
732-
writer.WriteProperty(OpenApiConstants.Description, Description);
733-
734737
// default
735738
writer.WriteOptionalObject(OpenApiConstants.Default, Default, (w, d) => w.WriteAny(d));
736739

@@ -779,9 +782,6 @@ internal void WriteAsSchemaProperties(
779782
// enum
780783
writer.WriteOptionalCollection(OpenApiConstants.Enum, Enum, (w, s) => w.WriteAny(new OpenApiAny(s)));
781784

782-
// type
783-
writer.WriteProperty(OpenApiConstants.Type, (string)Type);
784-
785785
// items
786786
writer.WriteOptionalObject(OpenApiConstants.Items, Items, (w, s) => s.SerializeAsV2(w));
787787

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using Json.Schema;
76
using Microsoft.OpenApi.Any;
87
using Microsoft.OpenApi.Interfaces;
98
using Microsoft.OpenApi.Writers;
@@ -86,7 +85,7 @@ public override string Description
8685
public override bool AllowEmptyValue { get => Target.AllowEmptyValue; set => Target.AllowEmptyValue = value; }
8786

8887
/// <inheritdoc/>
89-
public override JsonSchema Schema { get => Target.Schema; set => Target.Schema = value; }
88+
public override OpenApiSchema Schema { get => Target.Schema; set => Target.Schema = value; }
9089

9190
/// <inheritdoc/>
9291
public override ParameterStyle? Style { get => Target.Style; set => Target.Style = value; }

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using Json.Schema;
76
using Microsoft.OpenApi.Any;
87
using Microsoft.OpenApi.Interfaces;
98
using Microsoft.OpenApi.Writers;
@@ -94,7 +93,7 @@ public override string Description
9493
public override bool AllowReserved { get => Target.AllowReserved; set => Target.AllowReserved = value; }
9594

9695
/// <inheritdoc/>
97-
public override JsonSchema Schema { get => Target.Schema; set => Target.Schema = value; }
96+
public override OpenApiSchema Schema { get => Target.Schema; set => Target.Schema = value; }
9897

9998
/// <inheritdoc/>
10099
public override IDictionary<string, OpenApiExample> Examples { get => Target.Examples; set => Target.Examples = value; }

src/Microsoft.OpenApi/Reader/ParseNodes/AnyFieldMapParameter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Licensed under the MIT license.
33

44
using System;
5-
using Json.Schema;
65
using Microsoft.OpenApi.Any;
6+
using Microsoft.OpenApi.Models;
77

88
namespace Microsoft.OpenApi.Reader.ParseNodes
99
{
@@ -15,7 +15,7 @@ internal class AnyFieldMapParameter<T>
1515
public AnyFieldMapParameter(
1616
Func<T, OpenApiAny> propertyGetter,
1717
Action<T, OpenApiAny> propertySetter,
18-
Func<T, JsonSchema> SchemaGetter = null)
18+
Func<T, OpenApiSchema> SchemaGetter = null)
1919
{
2020
this.PropertyGetter = propertyGetter;
2121
this.PropertySetter = propertySetter;
@@ -35,6 +35,6 @@ public AnyFieldMapParameter(
3535
/// <summary>
3636
/// Function to get the schema to apply to the property.
3737
/// </summary>
38-
public Func<T, JsonSchema> SchemaGetter { get; }
38+
public Func<T, OpenApiSchema> SchemaGetter { get; }
3939
}
4040
}

src/Microsoft.OpenApi/Reader/ParseNodes/AnyListFieldMapParameter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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;
66
using System.Text.Json.Nodes;
7-
using Json.Schema;
7+
using Microsoft.OpenApi.Models;
88

99
namespace Microsoft.OpenApi.Reader.ParseNodes
1010
{
@@ -16,7 +16,7 @@ internal class AnyListFieldMapParameter<T>
1616
public AnyListFieldMapParameter(
1717
Func<T, IList<JsonNode>> propertyGetter,
1818
Action<T, IList<JsonNode>> propertySetter,
19-
Func<T, JsonSchema> SchemaGetter = null)
19+
Func<T, OpenApiSchema> SchemaGetter = null)
2020
{
2121
this.PropertyGetter = propertyGetter;
2222
this.PropertySetter = propertySetter;
@@ -36,6 +36,6 @@ public AnyListFieldMapParameter(
3636
/// <summary>
3737
/// Function to get the schema to apply to the property.
3838
/// </summary>
39-
public Func<T, JsonSchema> SchemaGetter { get; }
39+
public Func<T, OpenApiSchema> SchemaGetter { get; }
4040
}
4141
}

src/Microsoft.OpenApi/Reader/ParseNodes/AnyMapFieldMapParameter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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 Json.Schema;
76
using Microsoft.OpenApi.Any;
7+
using Microsoft.OpenApi.Models;
88

99
namespace Microsoft.OpenApi.Reader.ParseNodes
1010
{
@@ -17,7 +17,7 @@ public AnyMapFieldMapParameter(
1717
Func<T, IDictionary<string, U>> propertyMapGetter,
1818
Func<U, OpenApiAny> propertyGetter,
1919
Action<U, OpenApiAny> propertySetter,
20-
Func<T, JsonSchema> schemaGetter)
20+
Func<T, OpenApiSchema> schemaGetter)
2121
{
2222
this.PropertyMapGetter = propertyMapGetter;
2323
this.PropertyGetter = propertyGetter;
@@ -43,6 +43,6 @@ public AnyMapFieldMapParameter(
4343
/// <summary>
4444
/// Function to get the schema to apply to the property.
4545
/// </summary>
46-
public Func<T, JsonSchema> SchemaGetter { get; }
46+
public Func<T, OpenApiSchema> SchemaGetter { get; }
4747
}
4848
}

0 commit comments

Comments
 (0)