Skip to content

Commit 3d76e85

Browse files
committed
chore: linting use switch expressions
Signed-off-by: Vincent Biret <[email protected]>
1 parent b66ae01 commit 3d76e85

File tree

2 files changed

+70
-137
lines changed

2 files changed

+70
-137
lines changed

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiExampleGenerator.cs

Lines changed: 35 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Globalization;
910
using System.Linq;
1011
using System.Text.Json.Nodes;
1112
using Microsoft.OData.Edm;
@@ -75,33 +76,24 @@ private static OpenApiExample CreateExample(this ODataContext context, IEdmType
7576

7677
private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType structuredType)
7778
{
78-
OpenApiExample example = new OpenApiExample();
79+
OpenApiExample example = new();
7980

80-
JsonObject value = new JsonObject();
81-
82-
IEdmEntityType entityType = structuredType as IEdmEntityType;
81+
JsonObject value = new();
8382

8483
// properties
8584
foreach (var property in structuredType.DeclaredProperties.OrderBy(static p => p.Name, StringComparer.Ordinal))
8685
{
87-
// IOpenApiAny item;
8886
IEdmTypeReference propertyType = property.Type;
8987

9088
JsonNode item = GetTypeNameForExample(propertyType);
9189

92-
EdmTypeKind typeKind = propertyType.TypeKind();
93-
if (typeKind == EdmTypeKind.Primitive && item is JsonValue jsonValue && jsonValue.TryGetValue(out string stringAny))
90+
if (propertyType.TypeKind() == EdmTypeKind.Primitive &&
91+
item is JsonValue jsonValue &&
92+
jsonValue.TryGetValue(out string stringAny) &&
93+
structuredType is IEdmEntityType entityType &&
94+
entityType.Key().Any(k => StringComparer.Ordinal.Equals(k.Name, property.Name)))
9495
{
95-
string propertyValue = stringAny;
96-
if (entityType != null && entityType.Key().Any(k => k.Name == property.Name))
97-
{
98-
propertyValue += " (identifier)";
99-
}
100-
if (propertyType.IsDateTimeOffset() || propertyType.IsDate() || propertyType.IsTimeOfDay())
101-
{
102-
propertyValue += " (timestamp)";
103-
}
104-
item = propertyValue;
96+
item = $"{stringAny} (identifier)";
10597
}
10698

10799
value.Add(property.Name, item);
@@ -112,81 +104,33 @@ private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType str
112104

113105
private static JsonNode GetTypeNameForExample(IEdmTypeReference edmTypeReference)
114106
{
115-
switch (edmTypeReference.TypeKind())
107+
return edmTypeReference.TypeKind() switch
116108
{
117-
case EdmTypeKind.Primitive:
118-
if (edmTypeReference.IsBinary())
119-
{
120-
// return new OpenApiBinary(new byte[] { 0x00 }); issue on binary writing
121-
return Convert.ToBase64String(new byte[] { 0x00 });
122-
}
123-
else if (edmTypeReference.IsBoolean())
124-
{
125-
return true;
126-
}
127-
else if (edmTypeReference.IsByte())
128-
{
129-
return 0x00;
130-
}
131-
else if (edmTypeReference.IsDate())
132-
{
133-
return DateTime.MinValue;
134-
}
135-
else if (edmTypeReference.IsDateTimeOffset())
136-
{
137-
return DateTimeOffset.MinValue;
138-
}
139-
else if (edmTypeReference.IsDecimal() || edmTypeReference.IsDouble())
140-
{
141-
return 0D;
142-
}
143-
else if (edmTypeReference.IsFloating())
144-
{
145-
return 0F;
146-
}
147-
else if (edmTypeReference.IsGuid())
148-
{
149-
return Guid.Empty.ToString();
150-
}
151-
else if (edmTypeReference.IsInt16() || edmTypeReference.IsInt32())
152-
{
153-
return 0;
154-
}
155-
else if (edmTypeReference.IsInt64())
156-
{
157-
return 0L;
158-
}
159-
else
160-
{
161-
return edmTypeReference.AsPrimitive().PrimitiveDefinition().Name;
162-
}
163-
164-
case EdmTypeKind.Entity:
165-
case EdmTypeKind.Complex:
166-
case EdmTypeKind.Enum:
167-
JsonObject obj = new()
168-
{
169-
["@odata.type"] = edmTypeReference.FullName()
170-
};
171-
return obj;
172-
173-
case EdmTypeKind.Collection:
174-
JsonArray array = [];
175-
IEdmTypeReference elementType = edmTypeReference.AsCollection().ElementType();
176-
array.Add(GetTypeNameForExample(elementType));
177-
return array;
178-
179-
case EdmTypeKind.TypeDefinition:
180-
var typedef = edmTypeReference.AsTypeDefinition().TypeDefinition();
181-
return GetTypeNameForExample(new EdmPrimitiveTypeReference(typedef.UnderlyingType, edmTypeReference.IsNullable));
182-
183-
case EdmTypeKind.Untyped:
184-
return new JsonObject();
185-
186-
case EdmTypeKind.EntityReference:
187-
default:
188-
throw new OpenApiException("Not support for the type kind " + edmTypeReference.TypeKind());
189-
}
109+
// return new OpenApiBinary(new byte[] { 0x00 }); issue on binary writing
110+
EdmTypeKind.Primitive when edmTypeReference.IsBinary() => Convert.ToBase64String(new byte[] { 0x00 }),
111+
EdmTypeKind.Primitive when edmTypeReference.IsBoolean() => true,
112+
EdmTypeKind.Primitive when edmTypeReference.IsByte() => 0x00,
113+
EdmTypeKind.Primitive when edmTypeReference.IsDate() => DateTime.MinValue,
114+
EdmTypeKind.Primitive when edmTypeReference.IsDateTimeOffset() => DateTimeOffset.MinValue,
115+
EdmTypeKind.Primitive when edmTypeReference.IsDecimal() || edmTypeReference.IsDouble() => 0D,
116+
EdmTypeKind.Primitive when edmTypeReference.IsFloating() => 0F,
117+
EdmTypeKind.Primitive when edmTypeReference.IsGuid() => Guid.Empty.ToString(),
118+
EdmTypeKind.Primitive when edmTypeReference.IsInt16() || edmTypeReference.IsInt32() => 0,
119+
EdmTypeKind.Primitive when edmTypeReference.IsInt64() => 0L,
120+
EdmTypeKind.Primitive => edmTypeReference.AsPrimitive().PrimitiveDefinition().Name,
121+
EdmTypeKind.Entity or EdmTypeKind.Complex or EdmTypeKind.Enum => new JsonObject()
122+
{//TODO this is wrong for enums, and should instead use one of the enum members
123+
[Constants.OdataType] = edmTypeReference.FullName()
124+
},
125+
126+
EdmTypeKind.Collection => new JsonArray(GetTypeNameForExample(edmTypeReference.AsCollection().ElementType())),
127+
128+
EdmTypeKind.TypeDefinition => GetTypeNameForExample(new EdmPrimitiveTypeReference(edmTypeReference.AsTypeDefinition().TypeDefinition().UnderlyingType, edmTypeReference.IsNullable)),
129+
130+
EdmTypeKind.Untyped => new JsonObject(),
131+
132+
_ => throw new OpenApiException("Not support for the type kind " + edmTypeReference.TypeKind()),
133+
};
190134
}
191135
}
192136
}

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@ private static JsonObject CreateStructuredTypePropertiesExample(ODataContext con
614614
// properties
615615
foreach (var property in structuredType.Properties())
616616
{
617-
// IOpenApiAny item;
618617
IEdmTypeReference propertyType = property.Type;
619618

620619
JsonNode item = GetTypeNameForExample(context, propertyType, document);
@@ -640,57 +639,47 @@ private static JsonObject CreateStructuredTypePropertiesExample(ODataContext con
640639
return example;
641640
}
642641

643-
private static JsonNode GetTypeNameForExample(ODataContext context, IEdmTypeReference edmTypeReference, OpenApiDocument document)
642+
private static JsonNode GetTypeNameForPrimitive(ODataContext context, IEdmTypeReference edmTypeReference, OpenApiDocument document)
644643
{
645-
switch (edmTypeReference.TypeKind())
644+
IEdmPrimitiveType primitiveType = edmTypeReference.AsPrimitive().PrimitiveDefinition();
645+
OpenApiSchema schema = context.CreateSchema(primitiveType, document);
646+
647+
if (edmTypeReference.IsBoolean())
648+
{
649+
return true;
650+
}
651+
else
646652
{
647-
case EdmTypeKind.Primitive:
648-
IEdmPrimitiveType primitiveType = edmTypeReference.AsPrimitive().PrimitiveDefinition();
649-
OpenApiSchema schema = context.CreateSchema(primitiveType, document);
653+
if (schema.Reference != null)
654+
{
655+
return schema.Reference.Id;
656+
}
657+
else
658+
{
659+
return schema.Type.ToIdentifier() ??
660+
(schema.AnyOf ?? Enumerable.Empty<OpenApiSchema>())
661+
.Union(schema.AllOf ?? Enumerable.Empty<OpenApiSchema>())
662+
.Union(schema.OneOf ?? Enumerable.Empty<OpenApiSchema>())
663+
.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? schema.Format;
664+
}
665+
}
666+
}
650667

651-
if (edmTypeReference.IsBoolean())
652-
{
653-
return true;
654-
}
655-
else
656-
{
657-
if (schema.Reference != null)
658-
{
659-
return schema.Reference.Id;
660-
}
661-
else
662-
{
663-
return schema.Type.ToIdentifier() ??
664-
(schema.AnyOf ?? Enumerable.Empty<OpenApiSchema>())
665-
.Union(schema.AllOf ?? Enumerable.Empty<OpenApiSchema>())
666-
.Union(schema.OneOf ?? Enumerable.Empty<OpenApiSchema>())
667-
.FirstOrDefault(static x => !string.IsNullOrEmpty(x.Format))?.Format ?? schema.Format;
668-
}
669-
}
668+
private static JsonNode GetTypeNameForExample(ODataContext context, IEdmTypeReference edmTypeReference, OpenApiDocument document)
669+
{
670+
return edmTypeReference.TypeKind() switch
671+
{
672+
EdmTypeKind.Primitive => GetTypeNameForPrimitive(context, edmTypeReference, document),
670673

671-
case EdmTypeKind.Entity:
672-
case EdmTypeKind.Complex:
673-
case EdmTypeKind.Enum:
674-
JsonObject obj = new()
675-
{
674+
EdmTypeKind.Entity or EdmTypeKind.Complex or EdmTypeKind.Enum => new JsonObject()
675+
{//TODO this is wrong for enums, and should instead use one of the enum members
676676
[Constants.OdataType] = edmTypeReference.FullName()
677-
};
678-
return obj;
679-
680-
case EdmTypeKind.Collection:
681-
JsonArray array = [];
682-
IEdmTypeReference elementType = edmTypeReference.AsCollection().ElementType();
683-
array.Add(GetTypeNameForExample(context, elementType, document));
684-
return array;
685-
686-
case EdmTypeKind.Untyped:
687-
return new JsonObject();
677+
},
688678

689-
case EdmTypeKind.TypeDefinition:
690-
case EdmTypeKind.EntityReference:
691-
default:
692-
throw new OpenApiException("Not support for the type kind " + edmTypeReference.TypeKind());
693-
}
679+
EdmTypeKind.Collection => new JsonArray(GetTypeNameForExample(context, edmTypeReference.AsCollection().ElementType(), document)),
680+
EdmTypeKind.Untyped => new JsonObject(),
681+
_ => throw new OpenApiException("Not support for the type kind " + edmTypeReference.TypeKind()),
682+
};
694683
}
695684

696685
private static JsonNode CreateDefault(this IEdmStructuralProperty property)

0 commit comments

Comments
 (0)