Skip to content

Commit 542bddc

Browse files
authored
Merge branch 'vnext' into 458
2 parents bd19a68 + 8851cf3 commit 542bddc

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/OpenApiAnyConverter.cs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,61 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
5656
}
5757

5858
var value = ((OpenApiString)openApiAny).Value;
59+
var type = schema?.Type;
60+
var format = schema?.Format;
5961

60-
// For explicit strings only try to guess if it's a DateTimeOffset
6162
if (((OpenApiString)openApiAny).IsExplicit())
6263
{
63-
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
64+
// More narrow type detection for explicit strings, only check types that are passed as strings
65+
if (schema == null)
6466
{
65-
return new OpenApiDateTime(dateTimeValue);
67+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
68+
{
69+
return new OpenApiDateTime(dateTimeValue);
70+
}
71+
}
72+
else if (type == "string")
73+
{
74+
if (format == "byte")
75+
{
76+
try
77+
{
78+
return new OpenApiByte(Convert.FromBase64String(value));
79+
}
80+
catch (FormatException)
81+
{ }
82+
}
83+
84+
if (format == "binary")
85+
{
86+
try
87+
{
88+
return new OpenApiBinary(Encoding.UTF8.GetBytes(value));
89+
}
90+
catch (EncoderFallbackException)
91+
{ }
92+
}
93+
94+
if (format == "date")
95+
{
96+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateValue))
97+
{
98+
return new OpenApiDate(dateValue.Date);
99+
}
100+
}
101+
102+
if (format == "date-time")
103+
{
104+
if (DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTimeValue))
105+
{
106+
return new OpenApiDateTime(dateTimeValue);
107+
}
108+
}
109+
110+
if (format == "password")
111+
{
112+
return new OpenApiPassword(value);
113+
}
66114
}
67115

68116
return openApiAny;
@@ -107,9 +155,6 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
107155
}
108156
else
109157
{
110-
var type = schema.Type;
111-
var format = schema.Format;
112-
113158
if (type == "integer" && format == "int32")
114159
{
115160
if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue))
@@ -202,7 +247,7 @@ public static IOpenApiAny GetSpecificOpenApiAny(IOpenApiAny openApiAny, OpenApiS
202247

203248
if (type == "string")
204249
{
205-
return new OpenApiString(value);
250+
return openApiAny;
206251
}
207252

208253
if (type == "boolean")

0 commit comments

Comments
 (0)