Skip to content

Commit 3330982

Browse files
Fix [AsParameter] usage (#2980)
Fix incorrect types being generated with use of `[AsParameter]`.
1 parent 1d99754 commit 3330982

File tree

7 files changed

+418
-8
lines changed

7 files changed

+418
-8
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
407407
if (apiParameter is not null)
408408
{
409409
parameter.Schema = GenerateSchema(
410-
apiParameter.ModelMetadata.ModelType,
410+
apiParameter.Type,
411411
schemaRepository,
412412
apiParameter.PropertyInfo(),
413413
apiParameter.ParameterInfo(),
@@ -568,9 +568,19 @@ private OpenApiParameter GenerateParameterWithoutFilter(
568568

569569
var isRequired = apiParameter.IsRequiredParameter();
570570

571-
var schema = (apiParameter.ModelMetadata != null)
571+
var type = apiParameter.Type;
572+
573+
if (type is not null
574+
&& type == typeof(string)
575+
&& apiParameter.ModelMetadata?.ModelType is not null
576+
&& apiParameter.ModelMetadata.ModelType != type)
577+
{
578+
type = apiParameter.ModelMetadata.ModelType;
579+
}
580+
581+
var schema = (type != null)
572582
? GenerateSchema(
573-
apiParameter.ModelMetadata.ModelType,
583+
type,
574584
schemaRepository,
575585
apiParameter.PropertyInfo(),
576586
apiParameter.ParameterInfo(),

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,128 @@
4848
}
4949
}
5050
},
51+
"/annotations/AsParameters": {
52+
"get": {
53+
"tags": [
54+
"Annotations"
55+
],
56+
"parameters": [
57+
{
58+
"name": "paramOne",
59+
"in": "query",
60+
"description": "Description",
61+
"schema": {
62+
"type": "string",
63+
"format": "uuid"
64+
}
65+
},
66+
{
67+
"name": "paramTwo",
68+
"in": "query",
69+
"required": true,
70+
"schema": {
71+
"type": "string",
72+
"format": "uuid"
73+
}
74+
},
75+
{
76+
"name": "paramThree",
77+
"in": "query",
78+
"schema": {
79+
"type": "string",
80+
"format": "date-time"
81+
}
82+
},
83+
{
84+
"name": "paramFour",
85+
"in": "query",
86+
"required": true,
87+
"schema": {
88+
"type": "string",
89+
"format": "date-time"
90+
}
91+
},
92+
{
93+
"name": "paramFive",
94+
"in": "query",
95+
"schema": {
96+
"type": "string",
97+
"format": "date"
98+
}
99+
},
100+
{
101+
"name": "paramSix",
102+
"in": "query",
103+
"required": true,
104+
"schema": {
105+
"type": "string",
106+
"format": "date"
107+
}
108+
},
109+
{
110+
"name": "paramSeven",
111+
"in": "query",
112+
"schema": {
113+
"type": "string",
114+
"format": "time"
115+
}
116+
},
117+
{
118+
"name": "paramEight",
119+
"in": "query",
120+
"required": true,
121+
"schema": {
122+
"type": "string",
123+
"format": "time"
124+
}
125+
},
126+
{
127+
"name": "paramNine",
128+
"in": "query",
129+
"schema": {
130+
"$ref": "#/components/schemas/DateTimeKind"
131+
}
132+
},
133+
{
134+
"name": "paramTen",
135+
"in": "query",
136+
"required": true,
137+
"schema": {
138+
"$ref": "#/components/schemas/DateTimeKind"
139+
}
140+
},
141+
{
142+
"name": "paramEleven",
143+
"in": "query",
144+
"schema": {
145+
"type": "number",
146+
"format": "double"
147+
}
148+
},
149+
{
150+
"name": "paramTwelve",
151+
"in": "query",
152+
"required": true,
153+
"schema": {
154+
"type": "number",
155+
"format": "double"
156+
}
157+
}
158+
],
159+
"responses": {
160+
"200": {
161+
"description": "OK",
162+
"content": {
163+
"application/json": {
164+
"schema": {
165+
"$ref": "#/components/schemas/AsParametersRecord"
166+
}
167+
}
168+
}
169+
}
170+
}
171+
}
172+
},
51173
"/WithOpenApi/weatherforecast": {
52174
"get": {
53175
"tags": [
@@ -266,6 +388,72 @@
266388
},
267389
"additionalProperties": false
268390
},
391+
"AsParametersRecord": {
392+
"type": "object",
393+
"properties": {
394+
"paramOne": {
395+
"type": "string",
396+
"format": "uuid",
397+
"nullable": true
398+
},
399+
"paramTwo": {
400+
"type": "string",
401+
"format": "uuid"
402+
},
403+
"paramThree": {
404+
"type": "string",
405+
"format": "date-time",
406+
"nullable": true
407+
},
408+
"paramFour": {
409+
"type": "string",
410+
"format": "date-time"
411+
},
412+
"paramFive": {
413+
"type": "string",
414+
"format": "date",
415+
"nullable": true
416+
},
417+
"paramSix": {
418+
"type": "string",
419+
"format": "date"
420+
},
421+
"paramSeven": {
422+
"type": "string",
423+
"format": "time",
424+
"nullable": true
425+
},
426+
"paramEight": {
427+
"type": "string",
428+
"format": "time"
429+
},
430+
"paramNine": {
431+
"$ref": "#/components/schemas/DateTimeKind"
432+
},
433+
"paramTen": {
434+
"$ref": "#/components/schemas/DateTimeKind"
435+
},
436+
"paramEleven": {
437+
"type": "number",
438+
"format": "double",
439+
"nullable": true
440+
},
441+
"paramTwelve": {
442+
"type": "number",
443+
"format": "double"
444+
}
445+
},
446+
"additionalProperties": false
447+
},
448+
"DateTimeKind": {
449+
"enum": [
450+
0,
451+
1,
452+
2
453+
],
454+
"type": "integer",
455+
"format": "int32"
456+
},
269457
"Fruit": {
270458
"type": "object",
271459
"properties": {

0 commit comments

Comments
 (0)