Skip to content

Commit

Permalink
Reduce code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Oct 18, 2023
1 parent 0cfe904 commit 2b21a91
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 189 deletions.
34 changes: 17 additions & 17 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiHeaderDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ internal static partial class OpenApiV2Deserializer
{
"type", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
o.Schema = GetOrCreateHeaderSchemaBuilder().Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
}
},
{
"format", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Format(n.GetScalarValue());
o.Schema = GetOrCreateHeaderSchemaBuilder().Format(n.GetScalarValue());
}
},
{
"items", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Items(LoadSchema(n));
o.Schema = GetOrCreateHeaderSchemaBuilder().Items(LoadSchema(n));
}
},
{
Expand All @@ -53,79 +53,79 @@ internal static partial class OpenApiV2Deserializer
{
"default", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Default(n.CreateAny().Node);
o.Schema = GetOrCreateHeaderSchemaBuilder().Default(n.CreateAny().Node);
}
},
{
"maximum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"exclusiveMaximum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).ExclusiveMaximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().ExclusiveMaximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"minimum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"exclusiveMinimum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).ExclusiveMinimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().ExclusiveMinimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"maxLength", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"minLength", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"pattern", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Pattern(n.GetScalarValue());
o.Schema = GetOrCreateHeaderSchemaBuilder().Pattern(n.GetScalarValue());
}
},
{
"maxItems", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MaxItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().MaxItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"minItems", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MinItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().MinItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"uniqueItems", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).UniqueItems(bool.Parse(n.GetScalarValue()));
o.Schema = GetOrCreateHeaderSchemaBuilder().UniqueItems(bool.Parse(n.GetScalarValue()));
}
},
{
"multipleOf", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MultipleOf(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateHeaderSchemaBuilder().MultipleOf(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"enum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Enum(n.CreateListOfAny()).Build();
o.Schema = GetOrCreateHeaderSchemaBuilder().Enum(n.CreateListOfAny()).Build();
}
}
};
Expand All @@ -135,7 +135,7 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n))}
};

private static JsonSchemaBuilder GetOrCreateSchemaBuilder(OpenApiHeader p)
private static JsonSchemaBuilder GetOrCreateHeaderSchemaBuilder()
{
_headerJsonSchemaBuilder ??= new JsonSchemaBuilder();
return _headerJsonSchemaBuilder;
Expand Down
59 changes: 12 additions & 47 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Microsoft.OpenApi.Readers.V2
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static readonly JsonSchemaBuilder builder = new();
private static JsonSchemaBuilder _parameterJsonSchemaBuilder;
private static FixedFieldMap<OpenApiParameter> _parameterFixedFields =

Check notice

Code scanning / CodeQL

Missed 'readonly' opportunity Note

Field '_parameterFixedFields' can be 'readonly'.
new FixedFieldMap<OpenApiParameter>
Expand Down Expand Up @@ -63,13 +62,13 @@ internal static partial class OpenApiV2Deserializer
{
"type", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
o.Schema = GetOrCreateParameterSchemaBuilder().Type(SchemaTypeConverter.ConvertToSchemaValueType(n.GetScalarValue()));
}
},
{
"items", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Items(LoadSchema(n));
o.Schema = GetOrCreateParameterSchemaBuilder().Items(LoadSchema(n));
}
},
{
Expand All @@ -81,55 +80,55 @@ internal static partial class OpenApiV2Deserializer
{
"format", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Format(n.GetScalarValue());
o.Schema = GetOrCreateParameterSchemaBuilder().Format(n.GetScalarValue());
}
},
{
"minimum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateParameterSchemaBuilder().Minimum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"maximum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateParameterSchemaBuilder().Maximum(decimal.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"maxLength", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateParameterSchemaBuilder().MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"minLength", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
o.Schema = GetOrCreateParameterSchemaBuilder().MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
}
},
{
"readOnly", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).ReadOnly(bool.Parse(n.GetScalarValue()));
o.Schema = GetOrCreateParameterSchemaBuilder().ReadOnly(bool.Parse(n.GetScalarValue()));
}
},
{
"default", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Default(n.CreateAny().Node);
o.Schema = GetOrCreateParameterSchemaBuilder().Default(n.CreateAny().Node);
}
},
{
"pattern", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Pattern(n.GetScalarValue());
o.Schema = GetOrCreateParameterSchemaBuilder().Pattern(n.GetScalarValue());
}
},
{
"enum", (o, n) =>
{
o.Schema = GetOrCreateSchemaBuilder(o).Enum(n.CreateListOfAny()).Build();
o.Schema = GetOrCreateParameterSchemaBuilder().Enum(n.CreateListOfAny()).Build();
}
},
{
Expand All @@ -146,40 +145,6 @@ internal static partial class OpenApiV2Deserializer
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, LoadExtension(p, n))}
};

private static readonly AnyFieldMap<OpenApiParameter> _parameterAnyFields =
new AnyFieldMap<OpenApiParameter>
{
{
OpenApiConstants.Default,
new AnyFieldMapParameter<OpenApiParameter>(
p => new OpenApiAny(p.Schema?.GetDefault()),
(p, v) => {
if (p.Schema != null || v != null)
{
p.Schema = GetOrCreateSchemaBuilder(p).Default(v.Node);
}
},
p => p.Schema)
}
};

private static readonly AnyListFieldMap<OpenApiParameter> _parameterAnyListFields =
new AnyListFieldMap<OpenApiParameter>
{
{
OpenApiConstants.Enum,
new AnyListFieldMapParameter<OpenApiParameter>(
p => p.Schema?.GetEnum().ToList(),
(p, v) => {
if (p.Schema != null || v != null && v.Count > 0)
{
p.Schema = GetOrCreateSchemaBuilder(p).Enum(v);
}
},
p => p.Schema)
},
};

private static void LoadStyle(OpenApiParameter p, string v)
{
switch (v)
Expand Down Expand Up @@ -209,7 +174,7 @@ private static void LoadStyle(OpenApiParameter p, string v)
}
}

private static JsonSchemaBuilder GetOrCreateSchemaBuilder(OpenApiParameter p)
private static JsonSchemaBuilder GetOrCreateParameterSchemaBuilder()
{
_parameterJsonSchemaBuilder ??= new JsonSchemaBuilder();
return _parameterJsonSchemaBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ internal static partial class OpenApiV31Deserializer
{
"tags", (o, n) => o.Tags = n.CreateSimpleList(
valueNode =>
LoadTagByReference(
valueNode.Context,
valueNode.GetScalarValue()))
LoadTagByReference(valueNode.GetScalarValue()))
},
{
"summary", (o, n) =>
Expand Down Expand Up @@ -105,9 +103,7 @@ internal static OpenApiOperation LoadOperation(ParseNode node)
return operation;
}

private static OpenApiTag LoadTagByReference(
ParsingContext context,
string tagName)
private static OpenApiTag LoadTagByReference(string tagName)
{
var tagObject = new OpenApiTag()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static OpenApiSecurityRequirement LoadSecurityRequirement(ParseNode node)
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
}

var scheme = LoadSecuritySchemeByReference(mapNode.Context, property.Name, summary, description);
var scheme = LoadSecuritySchemeByReference(property.Name, summary, description);

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

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

private static OpenApiSecurityScheme LoadSecuritySchemeByReference(
ParsingContext context,
string schemeName,
string summary = null,
string description = null)
Expand Down
36 changes: 0 additions & 36 deletions src/Microsoft.OpenApi.Readers/V31/OpenApiV31Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,42 +71,6 @@ private static void ProcessAnyFields<T>(
}
}

private static void ProcessAnyListFields<T>(
MapNode mapNode,
T domainObject,
AnyListFieldMap<T> anyListFieldMap)
{
foreach (var anyListFieldName in anyListFieldMap.Keys.ToList())
{
try
{
var newProperty = new List<JsonNode>();

mapNode.Context.StartObject(anyListFieldName);

var propertyGetter = anyListFieldMap[anyListFieldName].PropertyGetter(domainObject);
if (propertyGetter != null)
{
foreach (var propertyElement in propertyGetter)
{
newProperty.Add(propertyElement);
}

anyListFieldMap[anyListFieldName].PropertySetter(domainObject, newProperty);
}
}
catch (OpenApiException exception)
{
exception.Pointer = mapNode.Context.GetLocation();
mapNode.Context.Diagnostic.Errors.Add(new OpenApiError(exception));
}
finally
{
mapNode.Context.EndObject();
}
}
}

private static void ProcessAnyMapFields<T, U>(
MapNode mapNode,
T domainObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private OpenApiReference ParseLocalReference(string localReference, string summa
if (segments[2] == "pathItems")
{
refId = "/" + segments[3];
};
}

var parsedReference = new OpenApiReference
{
Expand Down
Loading

0 comments on commit 2b21a91

Please sign in to comment.