Skip to content

Commit 7f9d933

Browse files
committed
Added tests for "UseOneOfForPolymorphism"
1 parent d01c90a commit 7f9d933

File tree

122 files changed

+7650
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+7650
-12
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>9.2.0</VersionPrefix>
5+
<VersionPrefix>9.3.0</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/KeyedValueObjects/KeyedValueObjectSchemaFilter.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Extensions.Options;
12
using Microsoft.OpenApi.Models;
23
using Swashbuckle.AspNetCore.SwaggerGen;
34
using Thinktecture.Internal;
@@ -12,6 +13,19 @@ namespace Thinktecture.Swashbuckle.Internal.KeyedValueObjects;
1213
/// </summary>
1314
public class KeyedValueObjectSchemaFilter : IInternalKeyedValueObjectSchemaFilter
1415
{
16+
private readonly bool _clearAllOf;
17+
18+
/// <summary>
19+
/// This is an internal API that supports the Thinktecture.Runtime.Extensions infrastructure and not subject to
20+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
21+
/// any release. You should only use it directly in your code with extreme caution and knowing that
22+
/// doing so can result in application failures when updating to a new Thinktecture.Runtime.Extensions release.
23+
/// </summary>
24+
public KeyedValueObjectSchemaFilter(IOptions<ThinktectureSchemaFilterOptions> options)
25+
{
26+
_clearAllOf = options.Value.ClearAllOfOnKeyedTypes;
27+
}
28+
1529
/// <inheritdoc />
1630
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
1731
{
@@ -27,6 +41,9 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context, Metadata.Ke
2741
schema.Properties.Clear();
2842
schema.Required.Clear();
2943

44+
if (_clearAllOf)
45+
schema.AllOf?.Clear();
46+
3047
var keySchema = context.SchemaGenerator.GenerateSchema(metadata.KeyType, context.SchemaRepository);
3148

3249
schema.Type = keySchema.Type;

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/Internal/SmartEnums/SmartEnumSchemaFilterBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected SmartEnumSchemaFilterBase(
3030
{
3131
_valueFactoryProvider = valueFactoryProvider;
3232
_extension = options.Value.SmartEnumSchemaExtension.CreateSchemaExtension(serviceProvider);
33-
_clearAllOf = options.Value.ClearAllOf;
33+
_clearAllOf = options.Value.ClearAllOfOnKeyedTypes;
3434
}
3535

3636
/// <inheritdoc />
@@ -48,7 +48,7 @@ public void Apply(OpenApiSchema schema, SchemaFilterContext context, Metadata.Ke
4848
schema.Properties.Clear();
4949
schema.Required.Clear();
5050

51-
if(_clearAllOf)
51+
if (_clearAllOf)
5252
schema.AllOf.Clear();
5353

5454
var items = GetItems(metadata.Type, metadata.KeyType, metadata.Items.Value);

src/Thinktecture.Runtime.Extensions.Swashbuckle/Swashbuckle/ThinktectureSchemaFilterOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ThinktectureSchemaFilterOptions
2121
public RequiredMemberEvaluator RequiredMemberEvaluator { get; set; } = RequiredMemberEvaluator.FromDependencyInjection;
2222

2323
/// <summary>
24-
/// Clears 'allOf' from the schema if there is any.
24+
/// Clears 'allOf' from the schema of Smart Enums and keyed Value Objects, which is usually used for inheritance.
2525
/// </summary>
26-
public bool ClearAllOf { get; set; }
26+
public bool ClearAllOfOnKeyedTypes { get; set; }
2727
}

test/Thinktecture.Runtime.Extensions.Swashbuckle.Tests/Swashbuckle/Helpers/TestController.AdHocUnions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,29 @@ public class Nullable : ControllerBase
8989
}
9090
}
9191
}
92+
93+
// ReSharper disable once InconsistentNaming
94+
public static class Class_with_BaseClass
95+
{
96+
[Route("/")]
97+
public class Body : ControllerBase
98+
{
99+
[HttpPost("/test")]
100+
public TestUnion_class_string_int_with_base_class Get([FromBody] TestUnion_class_string_int_with_base_class value)
101+
{
102+
return value;
103+
}
104+
105+
[Route("/")]
106+
public class Nullable : ControllerBase
107+
{
108+
[HttpPost("/test")]
109+
public TestUnion_class_string_int_with_base_class? Get([FromBody] TestUnion_class_string_int_with_base_class? value = null)
110+
{
111+
return value;
112+
}
113+
}
114+
}
115+
}
92116
}
93117
}

test/Thinktecture.Runtime.Extensions.Swashbuckle.Tests/Swashbuckle/Helpers/TestController.ComplexValueObjects.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ public class Nullable : ControllerBase
110110
}
111111
}
112112

113+
// ReSharper disable once InconsistentNaming
114+
public static class Class_with_BaseClass
115+
{
116+
[Route("/")]
117+
public class Body : ControllerBase
118+
{
119+
[HttpPost("/test")]
120+
public Boundary_with_BaseClass Get([FromBody] Boundary_with_BaseClass value)
121+
{
122+
return value;
123+
}
124+
125+
[Route("/")]
126+
public class Nullable : ControllerBase
127+
{
128+
[HttpPost("/test")]
129+
public Boundary_with_BaseClass? Get([FromBody] Boundary_with_BaseClass? value = null)
130+
{
131+
return value;
132+
}
133+
}
134+
}
135+
}
136+
113137
public static class Struct
114138
{
115139
[Route("/")]

test/Thinktecture.Runtime.Extensions.Swashbuckle.Tests/Swashbuckle/Helpers/TestController.KeyedValueObjects.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,90 @@ public class Nullable : ControllerBase
8989
}
9090
}
9191
}
92+
93+
// ReSharper disable once InconsistentNaming
94+
public static class StringBased_with_BaseClass
95+
{
96+
[Route("/")]
97+
public class Route : ControllerBase
98+
{
99+
[HttpGet("/test/{value}")]
100+
public StringBasedReferenceValueObject_with_BaseClass Get(StringBasedReferenceValueObject_with_BaseClass value)
101+
{
102+
return value;
103+
}
104+
105+
[Route("/")]
106+
public class Nullable : ControllerBase
107+
{
108+
[HttpGet("/test/{value}")]
109+
public StringBasedReferenceValueObject_with_BaseClass? Get(StringBasedReferenceValueObject_with_BaseClass? value = null)
110+
{
111+
return value;
112+
}
113+
}
114+
}
115+
116+
[Route("/")]
117+
public class QueryString : ControllerBase
118+
{
119+
[HttpGet("/test")]
120+
public StringBasedReferenceValueObject_with_BaseClass Get(StringBasedReferenceValueObject_with_BaseClass value)
121+
{
122+
return value;
123+
}
124+
125+
[Route("/")]
126+
public class Nullable : ControllerBase
127+
{
128+
[HttpGet("/test")]
129+
public StringBasedReferenceValueObject_with_BaseClass? Get(StringBasedReferenceValueObject_with_BaseClass? value = null)
130+
{
131+
return value;
132+
}
133+
}
134+
}
135+
136+
[Route("/")]
137+
public class Body : ControllerBase
138+
{
139+
[HttpPost("/test")]
140+
public StringBasedReferenceValueObject_with_BaseClass Get([FromBody] StringBasedReferenceValueObject_with_BaseClass value)
141+
{
142+
return value;
143+
}
144+
145+
[Route("/")]
146+
public class Nullable : ControllerBase
147+
{
148+
[HttpPost("/test")]
149+
public StringBasedReferenceValueObject_with_BaseClass? Get([FromBody] StringBasedReferenceValueObject_with_BaseClass? value = null)
150+
{
151+
return value;
152+
}
153+
}
154+
}
155+
156+
[Route("/")]
157+
public class Form : ControllerBase
158+
{
159+
[HttpPost("/test")]
160+
public StringBasedReferenceValueObject_with_BaseClass Get([FromForm] StringBasedReferenceValueObject_with_BaseClass value)
161+
{
162+
return value;
163+
}
164+
165+
[Route("/")]
166+
public class Nullable : ControllerBase
167+
{
168+
[HttpPost("/test")]
169+
public StringBasedReferenceValueObject_with_BaseClass? Get([FromForm] StringBasedReferenceValueObject_with_BaseClass? value = null)
170+
{
171+
return value;
172+
}
173+
}
174+
}
175+
}
92176
}
93177

94178
public static class KeyedValueObjectStruct
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Test API",
5+
"version": "v1"
6+
},
7+
"paths": {
8+
"/test": {
9+
"post": {
10+
"tags": [
11+
"Tests"
12+
],
13+
"requestBody": {
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"allOf": [
18+
{
19+
"$ref": "#/components/schemas/TestUnion_class_string_int_with_base_class"
20+
}
21+
]
22+
}
23+
}
24+
}
25+
},
26+
"responses": {
27+
"200": {
28+
"description": "OK",
29+
"content": {
30+
"application/json": {
31+
"schema": {
32+
"$ref": "#/components/schemas/TestUnion_class_string_int_with_base_class"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
},
41+
"components": {
42+
"schemas": {
43+
"TestUnion_class_string_int_with_base_class": {
44+
"oneOf": [
45+
{
46+
"type": "string"
47+
},
48+
{
49+
"type": "integer",
50+
"format": "int32"
51+
}
52+
]
53+
}
54+
}
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Test API",
5+
"version": "v1"
6+
},
7+
"paths": {
8+
"/test": {
9+
"post": {
10+
"tags": [
11+
"Tests"
12+
],
13+
"requestBody": {
14+
"content": {
15+
"application/json": {
16+
"schema": {
17+
"allOf": [
18+
{
19+
"$ref": "#/components/schemas/TestUnion_class_string_int_with_base_class"
20+
}
21+
]
22+
}
23+
}
24+
}
25+
},
26+
"responses": {
27+
"200": {
28+
"description": "OK",
29+
"content": {
30+
"application/json": {
31+
"schema": {
32+
"$ref": "#/components/schemas/TestUnion_class_string_int_with_base_class"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
},
41+
"components": {
42+
"schemas": {
43+
"Boundary_with_BaseClass": {
44+
"type": "object",
45+
"allOf": [
46+
{
47+
"$ref": "#/components/schemas/TestBaseClass"
48+
}
49+
],
50+
"properties": {
51+
"lower": {
52+
"type": "number",
53+
"format": "double",
54+
"readOnly": true
55+
},
56+
"upper": {
57+
"type": "number",
58+
"format": "double",
59+
"readOnly": true
60+
}
61+
},
62+
"additionalProperties": false
63+
},
64+
"StringBasedReferenceValueObject_with_BaseClass": {
65+
"type": "string",
66+
"allOf": [
67+
{
68+
"$ref": "#/components/schemas/TestBaseClass"
69+
}
70+
]
71+
},
72+
"TestBaseClass": {
73+
"type": "object",
74+
"properties": {
75+
"testProperty": {
76+
"type": "string",
77+
"nullable": true,
78+
"readOnly": true
79+
}
80+
},
81+
"additionalProperties": false
82+
},
83+
"TestUnion_class_string_int_with_base_class": {
84+
"allOf": [
85+
{
86+
"$ref": "#/components/schemas/TestBaseClass"
87+
}
88+
],
89+
"oneOf": [
90+
{
91+
"type": "string"
92+
},
93+
{
94+
"type": "integer",
95+
"format": "int32"
96+
}
97+
]
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)