Skip to content

Commit cfd35a8

Browse files
committed
Refactor nullable schema mappings
1 parent 77cd25a commit cfd35a8

File tree

1 file changed

+14
-88
lines changed

1 file changed

+14
-88
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiTypeMapper.cs

Lines changed: 14 additions & 88 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;
@@ -29,93 +29,19 @@ public static class OpenApiTypeMapper
2929
[typeof(char)] = () => new JsonSchemaBuilder().Type(SchemaValueType.String).Format("string").Build(),
3030

3131
// Nullable types
32-
[typeof(bool?)] = () => new JsonSchemaBuilder()
33-
.AnyOf(
34-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
35-
new JsonSchemaBuilder().Type(SchemaValueType.Boolean).Build()
36-
).Build(),
37-
38-
[typeof(byte?)] = () => new JsonSchemaBuilder()
39-
.AnyOf(
40-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
41-
new JsonSchemaBuilder().Type(SchemaValueType.String).Build()
42-
)
43-
.Format("byte").Build(),
44-
45-
[typeof(int?)] = () => new JsonSchemaBuilder()
46-
.AnyOf(
47-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
48-
new JsonSchemaBuilder().Type(SchemaValueType.Integer).Build()
49-
)
50-
.Format("int32").Build(),
51-
52-
[typeof(uint?)] = () => new JsonSchemaBuilder().AnyOf(
53-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
54-
new JsonSchemaBuilder().Type(SchemaValueType.Integer).Build()
55-
)
56-
.Format("int32").Build(),
57-
58-
[typeof(long?)] = () => new JsonSchemaBuilder()
59-
.AnyOf(
60-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
61-
new JsonSchemaBuilder().Type(SchemaValueType.Integer).Build()
62-
)
63-
.Format("int64").Build(),
64-
65-
[typeof(ulong?)] = () => new JsonSchemaBuilder()
66-
.AnyOf(
67-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
68-
new JsonSchemaBuilder().Type(SchemaValueType.Integer).Build()
69-
)
70-
.Format("int64").Build(),
71-
72-
[typeof(float?)] = () => new JsonSchemaBuilder()
73-
.AnyOf(
74-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
75-
new JsonSchemaBuilder().Type(SchemaValueType.Integer).Build()
76-
)
77-
.Format("float").Build(),
78-
79-
[typeof(double?)] = () => new JsonSchemaBuilder()
80-
.AnyOf(
81-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
82-
new JsonSchemaBuilder().Type(SchemaValueType.Number).Build())
83-
.Format("double").Build(),
84-
85-
[typeof(decimal?)] = () => new JsonSchemaBuilder()
86-
.AnyOf(
87-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
88-
new JsonSchemaBuilder().Type(SchemaValueType.Number).Build()
89-
)
90-
.Format("double").Build(),
91-
92-
[typeof(DateTime?)] = () => new JsonSchemaBuilder()
93-
.AnyOf(
94-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
95-
new JsonSchemaBuilder().Type(SchemaValueType.String).Build()
96-
)
97-
.Format("date-time").Build(),
98-
99-
[typeof(DateTimeOffset?)] = () => new JsonSchemaBuilder()
100-
.AnyOf(
101-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
102-
new JsonSchemaBuilder().Type(SchemaValueType.String).Build()
103-
)
104-
.Format("date-time").Build(),
105-
106-
[typeof(Guid?)] = () => new JsonSchemaBuilder()
107-
.AnyOf(
108-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
109-
new JsonSchemaBuilder().Type(SchemaValueType.String).Build()
110-
)
111-
.Format("string").Build(),
112-
113-
[typeof(char?)] = () => new JsonSchemaBuilder()
114-
.AnyOf(
115-
new JsonSchemaBuilder().Type(SchemaValueType.Null).Build(),
116-
new JsonSchemaBuilder().Type(SchemaValueType.String).Build()
117-
)
118-
.Format("string").Build(),
32+
[typeof(bool?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Boolean).Build(),
33+
[typeof(byte?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.String).Format("byte").Build(),
34+
[typeof(int?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Integer).Format("int32").Build(),
35+
[typeof(uint?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Integer).Format("int32").Build(),
36+
[typeof(long?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Integer).Format("int64").Build(),
37+
[typeof(ulong?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Integer).Format("int64").Build(),
38+
[typeof(float?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Integer).Format("float").Build(),
39+
[typeof(double?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Number).Format("double").Build(),
40+
[typeof(decimal?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.Integer).Format("double").Build(),
41+
[typeof(DateTime?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.String).Format("date-time").Build(),
42+
[typeof(DateTimeOffset?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.String).Format("date-time").Build(),
43+
[typeof(Guid?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.String).Format("string").Build(),
44+
[typeof(char?)] = () => new JsonSchemaBuilder().Type(SchemaValueType.Null | SchemaValueType.String).Format("string").Build(),
11945

12046
[typeof(Uri)] = () => new JsonSchemaBuilder().Type(SchemaValueType.String).Format("uri").Build(), // Uri is treated as simple string
12147
[typeof(string)] = () => new JsonSchemaBuilder().Type(SchemaValueType.String).Build(),

0 commit comments

Comments
 (0)