Skip to content

Commit 2b21a91

Browse files
committed
Reduce code smells
1 parent 0cfe904 commit 2b21a91

19 files changed

+188
-189
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ internal static partial class OpenApiV2Deserializer
2929
{
3030
"type", (o, n) =>
3131
{
32-
o.Schema = GetOrCreateSchemaBuilder(o).Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
32+
o.Schema = GetOrCreateHeaderSchemaBuilder().Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
3333
}
3434
},
3535
{
3636
"format", (o, n) =>
3737
{
38-
o.Schema = GetOrCreateSchemaBuilder(o).Format(n.GetScalarValue());
38+
o.Schema = GetOrCreateHeaderSchemaBuilder().Format(n.GetScalarValue());
3939
}
4040
},
4141
{
4242
"items", (o, n) =>
4343
{
44-
o.Schema = GetOrCreateSchemaBuilder(o).Items(LoadSchema(n));
44+
o.Schema = GetOrCreateHeaderSchemaBuilder().Items(LoadSchema(n));
4545
}
4646
},
4747
{
@@ -53,79 +53,79 @@ internal static partial class OpenApiV2Deserializer
5353
{
5454
"default", (o, n) =>
5555
{
56-
o.Schema = GetOrCreateSchemaBuilder(o).Default(n.CreateAny().Node);
56+
o.Schema = GetOrCreateHeaderSchemaBuilder().Default(n.CreateAny().Node);
5757
}
5858
},
5959
{
6060
"maximum", (o, n) =>
6161
{
62-
o.Schema = GetOrCreateSchemaBuilder(o).Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
62+
o.Schema = GetOrCreateHeaderSchemaBuilder().Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
6363
}
6464
},
6565
{
6666
"exclusiveMaximum", (o, n) =>
6767
{
68-
o.Schema = GetOrCreateSchemaBuilder(o).ExclusiveMaximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
68+
o.Schema = GetOrCreateHeaderSchemaBuilder().ExclusiveMaximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
6969
}
7070
},
7171
{
7272
"minimum", (o, n) =>
7373
{
74-
o.Schema = GetOrCreateSchemaBuilder(o).Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
74+
o.Schema = GetOrCreateHeaderSchemaBuilder().Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
7575
}
7676
},
7777
{
7878
"exclusiveMinimum", (o, n) =>
7979
{
80-
o.Schema = GetOrCreateSchemaBuilder(o).ExclusiveMinimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
80+
o.Schema = GetOrCreateHeaderSchemaBuilder().ExclusiveMinimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
8181
}
8282
},
8383
{
8484
"maxLength", (o, n) =>
8585
{
86-
o.Schema = GetOrCreateSchemaBuilder(o).MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
86+
o.Schema = GetOrCreateHeaderSchemaBuilder().MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
8787
}
8888
},
8989
{
9090
"minLength", (o, n) =>
9191
{
92-
o.Schema = GetOrCreateSchemaBuilder(o).MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
92+
o.Schema = GetOrCreateHeaderSchemaBuilder().MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
9393
}
9494
},
9595
{
9696
"pattern", (o, n) =>
9797
{
98-
o.Schema = GetOrCreateSchemaBuilder(o).Pattern(n.GetScalarValue());
98+
o.Schema = GetOrCreateHeaderSchemaBuilder().Pattern(n.GetScalarValue());
9999
}
100100
},
101101
{
102102
"maxItems", (o, n) =>
103103
{
104-
o.Schema = GetOrCreateSchemaBuilder(o).MaxItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
104+
o.Schema = GetOrCreateHeaderSchemaBuilder().MaxItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
105105
}
106106
},
107107
{
108108
"minItems", (o, n) =>
109109
{
110-
o.Schema = GetOrCreateSchemaBuilder(o).MinItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
110+
o.Schema = GetOrCreateHeaderSchemaBuilder().MinItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
111111
}
112112
},
113113
{
114114
"uniqueItems", (o, n) =>
115115
{
116-
o.Schema = GetOrCreateSchemaBuilder(o).UniqueItems(bool.Parse(n.GetScalarValue()));
116+
o.Schema = GetOrCreateHeaderSchemaBuilder().UniqueItems(bool.Parse(n.GetScalarValue()));
117117
}
118118
},
119119
{
120120
"multipleOf", (o, n) =>
121121
{
122-
o.Schema = GetOrCreateSchemaBuilder(o).MultipleOf(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
122+
o.Schema = GetOrCreateHeaderSchemaBuilder().MultipleOf(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
123123
}
124124
},
125125
{
126126
"enum", (o, n) =>
127127
{
128-
o.Schema = GetOrCreateSchemaBuilder(o).Enum(n.CreateListOfAny()).Build();
128+
o.Schema = GetOrCreateHeaderSchemaBuilder().Enum(n.CreateListOfAny()).Build();
129129
}
130130
}
131131
};
@@ -135,7 +135,7 @@ internal static partial class OpenApiV2Deserializer
135135
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n))}
136136
};
137137

138-
private static JsonSchemaBuilder GetOrCreateSchemaBuilder(OpenApiHeader p)
138+
private static JsonSchemaBuilder GetOrCreateHeaderSchemaBuilder()
139139
{
140140
_headerJsonSchemaBuilder ??= new JsonSchemaBuilder();
141141
return _headerJsonSchemaBuilder;

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

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Microsoft.OpenApi.Readers.V2
1919
/// </summary>
2020
internal static partial class OpenApiV2Deserializer
2121
{
22-
private static readonly JsonSchemaBuilder builder = new();
2322
private static JsonSchemaBuilder _parameterJsonSchemaBuilder;
2423
private static FixedFieldMap<OpenApiParameter> _parameterFixedFields =
2524
new FixedFieldMap<OpenApiParameter>
@@ -63,13 +62,13 @@ internal static partial class OpenApiV2Deserializer
6362
{
6463
"type", (o, n) =>
6564
{
66-
o.Schema = GetOrCreateSchemaBuilder(o).Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
65+
o.Schema = GetOrCreateParameterSchemaBuilder().Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
6766
}
6867
},
6968
{
7069
"items", (o, n) =>
7170
{
72-
o.Schema = GetOrCreateSchemaBuilder(o).Items(LoadSchema(n));
71+
o.Schema = GetOrCreateParameterSchemaBuilder().Items(LoadSchema(n));
7372
}
7473
},
7574
{
@@ -81,55 +80,55 @@ internal static partial class OpenApiV2Deserializer
8180
{
8281
"format", (o, n) =>
8382
{
84-
o.Schema = GetOrCreateSchemaBuilder(o).Format(n.GetScalarValue());
83+
o.Schema = GetOrCreateParameterSchemaBuilder().Format(n.GetScalarValue());
8584
}
8685
},
8786
{
8887
"minimum", (o, n) =>
8988
{
90-
o.Schema = GetOrCreateSchemaBuilder(o).Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
89+
o.Schema = GetOrCreateParameterSchemaBuilder().Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
9190
}
9291
},
9392
{
9493
"maximum", (o, n) =>
9594
{
96-
o.Schema = GetOrCreateSchemaBuilder(o).Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
95+
o.Schema = GetOrCreateParameterSchemaBuilder().Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
9796
}
9897
},
9998
{
10099
"maxLength", (o, n) =>
101100
{
102-
o.Schema = GetOrCreateSchemaBuilder(o).MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
101+
o.Schema = GetOrCreateParameterSchemaBuilder().MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
103102
}
104103
},
105104
{
106105
"minLength", (o, n) =>
107106
{
108-
o.Schema = GetOrCreateSchemaBuilder(o).MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
107+
o.Schema = GetOrCreateParameterSchemaBuilder().MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
109108
}
110109
},
111110
{
112111
"readOnly", (o, n) =>
113112
{
114-
o.Schema = GetOrCreateSchemaBuilder(o).ReadOnly(bool.Parse(n.GetScalarValue()));
113+
o.Schema = GetOrCreateParameterSchemaBuilder().ReadOnly(bool.Parse(n.GetScalarValue()));
115114
}
116115
},
117116
{
118117
"default", (o, n) =>
119118
{
120-
o.Schema = GetOrCreateSchemaBuilder(o).Default(n.CreateAny().Node);
119+
o.Schema = GetOrCreateParameterSchemaBuilder().Default(n.CreateAny().Node);
121120
}
122121
},
123122
{
124123
"pattern", (o, n) =>
125124
{
126-
o.Schema = GetOrCreateSchemaBuilder(o).Pattern(n.GetScalarValue());
125+
o.Schema = GetOrCreateParameterSchemaBuilder().Pattern(n.GetScalarValue());
127126
}
128127
},
129128
{
130129
"enum", (o, n) =>
131130
{
132-
o.Schema = GetOrCreateSchemaBuilder(o).Enum(n.CreateListOfAny()).Build();
131+
o.Schema = GetOrCreateParameterSchemaBuilder().Enum(n.CreateListOfAny()).Build();
133132
}
134133
},
135134
{
@@ -146,40 +145,6 @@ internal static partial class OpenApiV2Deserializer
146145
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n))}
147146
};
148147

149-
private static readonly AnyFieldMap<OpenApiParameter> _parameterAnyFields =
150-
new AnyFieldMap<OpenApiParameter>
151-
{
152-
{
153-
OpenApiConstants.Default,
154-
new AnyFieldMapParameter<OpenApiParameter>(
155-
p => new OpenApiAny(p.Schema?.GetDefault()),
156-
(p, v) => {
157-
if (p.Schema != null || v != null)
158-
{
159-
p.Schema = GetOrCreateSchemaBuilder(p).Default(v.Node);
160-
}
161-
},
162-
p => p.Schema)
163-
}
164-
};
165-
166-
private static readonly AnyListFieldMap<OpenApiParameter> _parameterAnyListFields =
167-
new AnyListFieldMap<OpenApiParameter>
168-
{
169-
{
170-
OpenApiConstants.Enum,
171-
new AnyListFieldMapParameter<OpenApiParameter>(
172-
p => p.Schema?.GetEnum().ToList(),
173-
(p, v) => {
174-
if (p.Schema != null || v != null && v.Count > 0)
175-
{
176-
p.Schema = GetOrCreateSchemaBuilder(p).Enum(v);
177-
}
178-
},
179-
p => p.Schema)
180-
},
181-
};
182-
183148
private static void LoadStyle(OpenApiParameter p, string v)
184149
{
185150
switch (v)
@@ -209,7 +174,7 @@ private static void LoadStyle(OpenApiParameter p, string v)
209174
}
210175
}
211176

212-
private static JsonSchemaBuilder GetOrCreateSchemaBuilder(OpenApiParameter p)
177+
private static JsonSchemaBuilder GetOrCreateParameterSchemaBuilder()
213178
{
214179
_parameterJsonSchemaBuilder ??= new JsonSchemaBuilder();
215180
return _parameterJsonSchemaBuilder;

src/Microsoft.OpenApi.Readers/V31/OpenApiOperationDeserializer.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ internal static partial class OpenApiV31Deserializer
1616
{
1717
"tags", (o, n) => o.Tags = n.CreateSimpleList(
1818
valueNode =>
19-
LoadTagByReference(
20-
valueNode.Context,
21-
valueNode.GetScalarValue()))
19+
LoadTagByReference(valueNode.GetScalarValue()))
2220
},
2321
{
2422
"summary", (o, n) =>
@@ -105,9 +103,7 @@ internal static OpenApiOperation LoadOperation(ParseNode node)
105103
return operation;
106104
}
107105

108-
private static OpenApiTag LoadTagByReference(
109-
ParsingContext context,
110-
string tagName)
106+
private static OpenApiTag LoadTagByReference(string tagName)
111107
{
112108
var tagObject = new OpenApiTag()
113109
{

src/Microsoft.OpenApi.Readers/V31/OpenApiSecurityRequirementDeserializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static OpenApiSecurityRequirement LoadSecurityRequirement(ParseNode node)
2828
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
2929
}
3030

31-
var scheme = LoadSecuritySchemeByReference(mapNode.Context, property.Name, summary, description);
31+
var scheme = LoadSecuritySchemeByReference(property.Name, summary, description);
3232

3333
var scopes = property.Value.CreateSimpleList(value => value.GetScalarValue());
3434

@@ -47,7 +47,6 @@ public static OpenApiSecurityRequirement LoadSecurityRequirement(ParseNode node)
4747
}
4848

4949
private static OpenApiSecurityScheme LoadSecuritySchemeByReference(
50-
ParsingContext context,
5150
string schemeName,
5251
string summary = null,
5352
string description = null)

src/Microsoft.OpenApi.Readers/V31/OpenApiV31Deserializer.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,42 +71,6 @@ private static void ProcessAnyFields<T>(
7171
}
7272
}
7373

74-
private static void ProcessAnyListFields<T>(
75-
MapNode mapNode,
76-
T domainObject,
77-
AnyListFieldMap<T> anyListFieldMap)
78-
{
79-
foreach (var anyListFieldName in anyListFieldMap.Keys.ToList())
80-
{
81-
try
82-
{
83-
var newProperty = new List<JsonNode>();
84-
85-
mapNode.Context.StartObject(anyListFieldName);
86-
87-
var propertyGetter = anyListFieldMap[anyListFieldName].PropertyGetter(domainObject);
88-
if (propertyGetter != null)
89-
{
90-
foreach (var propertyElement in propertyGetter)
91-
{
92-
newProperty.Add(propertyElement);
93-
}
94-
95-
anyListFieldMap[anyListFieldName].PropertySetter(domainObject, newProperty);
96-
}
97-
}
98-
catch (OpenApiException exception)
99-
{
100-
exception.Pointer = mapNode.Context.GetLocation();
101-
mapNode.Context.Diagnostic.Errors.Add(new OpenApiError(exception));
102-
}
103-
finally
104-
{
105-
mapNode.Context.EndObject();
106-
}
107-
}
108-
}
109-
11074
private static void ProcessAnyMapFields<T, U>(
11175
MapNode mapNode,
11276
T domainObject,

src/Microsoft.OpenApi.Readers/V31/OpenApiV31VersionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private OpenApiReference ParseLocalReference(string localReference, string summa
193193
if (segments[2] == "pathItems")
194194
{
195195
refId = "/" + segments[3];
196-
};
196+
}
197197

198198
var parsedReference = new OpenApiReference
199199
{

0 commit comments

Comments
 (0)