Skip to content

Commit 3abe36c

Browse files
committed
Convert the JsonSchemaType helper methods to extensions
1 parent 07987ae commit 3abe36c

File tree

9 files changed

+16
-16
lines changed

9 files changed

+16
-16
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class OpenApiTypeMapper
1818
/// </summary>
1919
/// <param name="schemaType"></param>
2020
/// <returns></returns>
21-
public static string ToIdentifier(JsonSchemaType? schemaType)
21+
public static string ToIdentifier(this JsonSchemaType? schemaType)
2222
{
2323
return schemaType switch
2424
{
@@ -38,7 +38,7 @@ public static string ToIdentifier(JsonSchemaType? schemaType)
3838
/// </summary>
3939
/// <param name="identifier"></param>
4040
/// <returns></returns>
41-
public static JsonSchemaType IdentifierToEnumType(string identifier)
41+
public static JsonSchemaType ToJsonSchemaType(this string identifier)
4242
{
4343
return identifier switch
4444
{
@@ -137,7 +137,7 @@ public static Type MapOpenApiPrimitiveTypeToSimpleType(this OpenApiSchema schema
137137
throw new ArgumentNullException(nameof(schema));
138138
}
139139

140-
var type = (ToIdentifier(schema.Type), schema.Format?.ToLowerInvariant(), schema.Nullable) switch
140+
var type = (schema.Type.ToIdentifier(), schema.Format?.ToLowerInvariant(), schema.Nullable) switch
141141
{
142142
("boolean", null, false) => typeof(bool),
143143
// integer is technically not valid with format, but we must provide some compatibility

src/Microsoft.OpenApi/Models/OpenApiRequestBody.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ internal IEnumerable<OpenApiFormDataParameter> ConvertToFormDataParameters()
142142
foreach (var property in Content.First().Value.Schema.Properties)
143143
{
144144
var paramSchema = property.Value;
145-
if ("string".Equals(OpenApiTypeMapper.ToIdentifier(paramSchema.Type), StringComparison.OrdinalIgnoreCase)
145+
if ("string".Equals(paramSchema.Type.ToIdentifier(), StringComparison.OrdinalIgnoreCase)
146146
&& ("binary".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)
147147
|| "base64".Equals(paramSchema.Format, StringComparison.OrdinalIgnoreCase)))
148148
{
149-
paramSchema.Type = OpenApiTypeMapper.IdentifierToEnumType("file");
149+
paramSchema.Type = "file".ToJsonSchemaType();
150150
paramSchema.Format = null;
151151
}
152152
yield return new()

src/Microsoft.OpenApi/Models/OpenApiSchema.cs

Lines changed: 3 additions & 3 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;
@@ -577,7 +577,7 @@ internal void WriteV31Properties(IOpenApiWriter writer)
577577
internal void WriteAsItemsProperties(IOpenApiWriter writer)
578578
{
579579
// type
580-
writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(Type));
580+
writer.WriteProperty(OpenApiConstants.Type, Type.ToIdentifier());
581581

582582
// format
583583
if (string.IsNullOrEmpty(Format))
@@ -798,7 +798,7 @@ private void SerializeTypeProperty(JsonSchemaType? type, IOpenApiWriter writer,
798798
}
799799
else
800800
{
801-
writer.WriteProperty(OpenApiConstants.Type, OpenApiTypeMapper.ToIdentifier(type));
801+
writer.WriteProperty(OpenApiConstants.Type, type.ToIdentifier());
802802
}
803803
}
804804
else if(flagsCount > 1)

src/Microsoft.OpenApi/Reader/V2/OpenApiHeaderDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal static partial class OpenApiV2Deserializer
2424
},
2525
{
2626
"type",
27-
(o, n, _) => GetOrCreateSchema(o).Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue())
27+
(o, n, _) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType()
2828
},
2929
{
3030
"format",

src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal static partial class OpenApiV2Deserializer
4646
},
4747
{
4848
"type",
49-
(o, n, t) => GetOrCreateSchema(o).Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue())
49+
(o, n, t) => GetOrCreateSchema(o).Type = n.GetScalarValue().ToJsonSchemaType()
5050
},
5151
{
5252
"items",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal static partial class OpenApiV2Deserializer
8585

8686
{
8787
"type",
88-
(o, n, _) => o.Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue())
88+
(o, n, _) => o.Type = n.GetScalarValue().ToJsonSchemaType()
8989
},
9090
{
9191
"allOf",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ internal static partial class OpenApiV3Deserializer
8484
},
8585
{
8686
"type",
87-
(o, n, _) => o.Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue())
87+
(o, n, _) => o.Type = n.GetScalarValue().ToJsonSchemaType()
8888
},
8989
{
9090
"allOf",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ internal static partial class OpenApiV31Deserializer
116116
{
117117
if (n is ValueNode)
118118
{
119-
o.Type = OpenApiTypeMapper.IdentifierToEnumType(n.GetScalarValue());
119+
o.Type = n.GetScalarValue().ToJsonSchemaType();
120120
}
121121
else
122122
{
123123
var list = n.CreateSimpleList((n2, p) => n2.GetScalarValue());
124124
JsonSchemaType combinedType = 0;
125125
foreach(var type in list)
126126
{
127-
var schemaType = OpenApiTypeMapper.IdentifierToEnumType(type);
127+
var schemaType = type.ToJsonSchemaType();
128128
combinedType |= schemaType;
129129
}
130130
o.Type = combinedType;

src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs

Lines changed: 2 additions & 2 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.Text.Json;
@@ -54,7 +54,7 @@ public static void ValidateDataTypeMismatch(
5454
// convert value to JsonElement and access the ValueKind property to determine the type.
5555
var jsonElement = JsonDocument.Parse(JsonSerializer.Serialize(value)).RootElement;
5656

57-
var type = OpenApiTypeMapper.ToIdentifier(schema.Type);
57+
var type = schema.Type.ToIdentifier();
5858
var format = schema.Format;
5959
var nullable = schema.Nullable;
6060

0 commit comments

Comments
 (0)