-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSchema.OperationCompilationTests.fs
More file actions
1702 lines (1488 loc) · 52 KB
/
Schema.OperationCompilationTests.fs
File metadata and controls
1702 lines (1488 loc) · 52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module SwaggerProvider.Tests.Schema_OperationCompilationTests
/// Unit tests for the v3 OperationCompiler — verifying generated method signatures,
/// parameter ordering, CancellationToken injection, return-type resolution,
/// and async vs task mode.
open System
open System.Reflection
open System.Threading
open System.Threading.Tasks
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.ExprShape
open Microsoft.FSharp.Quotations.Patterns
open Xunit
open FsUnitTyped
// ── Helpers ───────────────────────────────────────────────────────────────────
let private compileTaskSchema schemaStr =
compileV3Schema schemaStr false
let private compileAsyncSchema schemaStr =
compileV3Schema schemaStr true
let private findMethod (types: ProviderImplementation.ProvidedTypes.ProvidedTypeDefinition list) (methodName: string) =
types
|> List.collect(fun t -> t.GetMethods() |> Array.toList)
|> List.tryFind(fun m -> m.Name = methodName)
let private getInvokeCode(method: MethodInfo) =
let providedMethod = method :?> ProviderImplementation.ProvidedTypes.ProvidedMethod
let invokeCodeProp =
providedMethod.GetType().GetProperty("GetInvokeCode", BindingFlags.Instance ||| BindingFlags.NonPublic)
if isNull invokeCodeProp then
failwith "GetInvokeCode property not found on ProvidedMethod"
match invokeCodeProp.GetValue(providedMethod) :?> (Expr list -> Expr) option with
| Some invokeCode -> invokeCode
| None -> failwith $"Method '%s{method.Name}' has no invoke code"
let private letBoundVars expr =
let rec loop expr =
match expr with
| Let(v, value, body) -> v :: (loop value @ loop body)
| ShapeVar _ -> []
| ShapeLambda(_, body) -> loop body
| ShapeCombination(_, args) -> args |> List.collect loop
loop expr
let private containsDuplicateVarObject vars =
vars
|> List.mapi(fun i v ->
vars
|> List.skip(i + 1)
|> List.exists(fun other -> obj.ReferenceEquals(v, other)))
|> List.exists id
// ── Simple GET with no parameters ─────────────────────────────────────────────
let private simpleGetSchema =
"""openapi: "3.0.0"
info:
title: SimpleGetTest
version: "1.0.0"
paths:
/status:
get:
operationId: getStatus
summary: Get server status
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``GET endpoint generates a method with the operation name``() =
let types = compileTaskSchema simpleGetSchema
let method = findMethod types "GetStatus"
method.IsSome |> shouldEqual true
[<Fact>]
let ``GET endpoint with no parameters has CancellationToken as its only parameter``() =
let types = compileTaskSchema simpleGetSchema
let method = (findMethod types "GetStatus").Value
let parameters = method.GetParameters()
parameters.Length |> shouldEqual 1
parameters[0].ParameterType |> shouldEqual typeof<CancellationToken>
[<Fact>]
let ``GET endpoint returning JSON string has Task<string> return type``() =
let types = compileTaskSchema simpleGetSchema
let method = (findMethod types "GetStatus").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Task<_>>
method.ReturnType.GetGenericArguments()[0]
|> shouldEqual typeof<string>
// ── GET with required and optional path/query parameters ──────────────────────
let private parametrisedGetSchema =
"""openapi: "3.0.0"
info:
title: ParameterisedGetTest
version: "1.0.0"
paths:
/items/{id}:
get:
operationId: getItem
summary: Get item by ID
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
- name: tag
in: query
required: false
schema:
type: string
responses:
"200":
description: OK
components:
schemas: {}
"""
[<Fact>]
let ``GET with required + optional params orders required before optional``() =
let types = compileTaskSchema parametrisedGetSchema
let method = (findMethod types "GetItem").Value
let parameters = method.GetParameters()
// Expected: id (required int64), tag (optional string), cancellationToken (CT)
parameters.Length |> shouldEqual 3
let idParam = parameters[0]
let tagParam = parameters[1]
let ctParam = parameters[2]
idParam.Name |> shouldEqual "id"
idParam.ParameterType |> shouldEqual typeof<int64>
// optional — marked as optional via ParameterAttributes
tagParam.Name |> shouldEqual "tag"
tagParam.IsOptional |> shouldEqual true
ctParam.ParameterType |> shouldEqual typeof<CancellationToken>
[<Fact>]
let ``CancellationToken is always the last parameter``() =
let types = compileTaskSchema parametrisedGetSchema
let method = (findMethod types "GetItem").Value
let parameters = method.GetParameters()
let last = parameters |> Array.last
last.ParameterType |> shouldEqual typeof<CancellationToken>
// ── POST with JSON request body ───────────────────────────────────────────────
let private postWithBodySchema =
"""openapi: "3.0.0"
info:
title: PostBodyTest
version: "1.0.0"
paths:
/items:
post:
operationId: createItem
summary: Create a new item
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NewItem'
responses:
"201":
description: Created
components:
schemas:
NewItem:
type: object
required:
- name
properties:
name:
type: string
"""
[<Fact>]
let ``POST with body generates method with body parameter before CancellationToken``() =
let types = compileTaskSchema postWithBodySchema
let method = (findMethod types "CreateItem").Value
let parameters = method.GetParameters()
// Expected: body (required NewItem), cancellationToken (CT)
parameters.Length |> shouldEqual 2
let bodyParam = parameters[0]
let ctParam = parameters[1]
// Body parameter is a provided type, so just verify it is not CancellationToken
bodyParam.ParameterType |> shouldNotEqual typeof<CancellationToken>
ctParam.ParameterType |> shouldEqual typeof<CancellationToken>
[<Fact>]
let ``POST with no response body has Task<unit> return type``() =
let types = compileTaskSchema postWithBodySchema
let method = (findMethod types "CreateItem").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Task<_>>
method.ReturnType.GetGenericArguments()[0] |> shouldEqual typeof<unit>
// ── CancellationToken naming collision avoidance ──────────────────────────────
let private ctCollisionSchema =
"""openapi: "3.0.0"
info:
title: CTCollisionTest
version: "1.0.0"
paths:
/search:
get:
operationId: search
parameters:
- name: cancellationToken
in: query
required: false
schema:
type: string
responses:
"200":
description: OK
components:
schemas: {}
"""
[<Fact>]
let ``when a query param is named cancellationToken the injected CT param gets a unique name``() =
let types = compileTaskSchema ctCollisionSchema
let method = (findMethod types "Search").Value
let parameters = method.GetParameters()
// There should be two parameters: the query param + the CT param
parameters.Length |> shouldEqual 2
let ctParam = parameters |> Array.last
// The injected CT param must not collide with the API param name
ctParam.ParameterType |> shouldEqual typeof<CancellationToken>
ctParam.Name |> shouldNotEqual parameters[0].Name
// ── Multiple operations — each gets its own CT parameter ─────────────────────
let private multiOpSchema =
"""openapi: "3.0.0"
info:
title: MultiOpTest
version: "1.0.0"
paths:
/pets:
get:
operationId: listPets
responses:
"200":
description: OK
post:
operationId: createPet
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
responses:
"201":
description: Created
components:
schemas: {}
"""
[<Fact>]
let ``multiple operations each generate a method with CancellationToken``() =
let types = compileTaskSchema multiOpSchema
let listPets = findMethod types "ListPets"
let createPet = findMethod types "CreatePet"
listPets.IsSome |> shouldEqual true
createPet.IsSome |> shouldEqual true
let listPetsParams = listPets.Value.GetParameters()
let createPetParams = createPet.Value.GetParameters()
(listPetsParams |> Array.last).ParameterType
|> shouldEqual typeof<CancellationToken>
(createPetParams |> Array.last).ParameterType
|> shouldEqual typeof<CancellationToken>
// ── Async mode: asAsync=true returns Async<T> instead of Task<T> ──────────────
[<Fact>]
let ``asAsync=true: GET returning string produces Async<string> return type``() =
let types = compileAsyncSchema simpleGetSchema
let method = (findMethod types "GetStatus").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Async<_>>
method.ReturnType.GetGenericArguments()[0]
|> shouldEqual typeof<string>
[<Fact>]
let ``asAsync=true: POST with no response body produces Async<unit> return type``() =
let types = compileAsyncSchema postWithBodySchema
let method = (findMethod types "CreateItem").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Async<_>>
method.ReturnType.GetGenericArguments()[0] |> shouldEqual typeof<unit>
[<Fact>]
let ``asAsync=true: method is still generated with correct name``() =
let types = compileAsyncSchema simpleGetSchema
let method = findMethod types "GetStatus"
method.IsSome |> shouldEqual true
[<Fact>]
let ``asAsync=true: CancellationToken is still the last parameter``() =
let types = compileAsyncSchema parametrisedGetSchema
let method = (findMethod types "GetItem").Value
let parameters = method.GetParameters()
(parameters |> Array.last).ParameterType
|> shouldEqual typeof<CancellationToken>
// ── DELETE / PUT operations ──────────────────────────────────────────────────
let private deleteEndpointSchema =
"""openapi: "3.0.0"
info:
title: DeleteTest
version: "1.0.0"
paths:
/items/{id}:
delete:
operationId: deleteItem
summary: Delete an item
parameters:
- name: id
in: path
required: true
schema:
type: integer
responses:
"204":
description: No Content
components:
schemas: {}
"""
[<Fact>]
let ``DELETE endpoint generates a method``() =
let types = compileTaskSchema deleteEndpointSchema
let method = findMethod types "DeleteItem"
method.IsSome |> shouldEqual true
[<Fact>]
let ``DELETE endpoint with 204 response produces Task<unit> return type``() =
let types = compileTaskSchema deleteEndpointSchema
let method = (findMethod types "DeleteItem").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Task<_>>
method.ReturnType.GetGenericArguments()[0] |> shouldEqual typeof<unit>
[<Fact>]
let ``DELETE endpoint path parameter is included before CancellationToken``() =
let types = compileTaskSchema deleteEndpointSchema
let method = (findMethod types "DeleteItem").Value
let parameters = method.GetParameters()
// id (required int32) + cancellationToken
parameters.Length |> shouldEqual 2
parameters[0].Name |> shouldEqual "id"
parameters[0].ParameterType |> shouldEqual typeof<int32>
(parameters |> Array.last).ParameterType
|> shouldEqual typeof<CancellationToken>
let private putEndpointSchema =
"""openapi: "3.0.0"
info:
title: PutTest
version: "1.0.0"
paths:
/items/{id}:
put:
operationId: updateItem
summary: Update an item
parameters:
- name: id
in: path
required: true
schema:
type: integer
format: int64
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/UpdateItem'
responses:
"200":
description: Updated
content:
application/json:
schema:
$ref: '#/components/schemas/UpdateItem'
components:
schemas:
UpdateItem:
type: object
required:
- name
properties:
name:
type: string
"""
[<Fact>]
let ``PUT endpoint generates a method``() =
let types = compileTaskSchema putEndpointSchema
let method = findMethod types "UpdateItem"
method.IsSome |> shouldEqual true
[<Fact>]
let ``PUT endpoint has path param and body param before CancellationToken``() =
let types = compileTaskSchema putEndpointSchema
let method = (findMethod types "UpdateItem").Value
let parameters = method.GetParameters()
// id (int64) + body (UpdateItem) + cancellationToken — 3 params total
parameters.Length |> shouldEqual 3
parameters[0].Name |> shouldEqual "id"
parameters[0].ParameterType |> shouldEqual typeof<int64>
// body param is a provided type (not CancellationToken)
parameters[1].ParameterType
|> shouldNotEqual typeof<CancellationToken>
(parameters |> Array.last).ParameterType
|> shouldEqual typeof<CancellationToken>
[<Fact>]
let ``PUT endpoint with JSON response produces Task<T> return type``() =
let types = compileTaskSchema putEndpointSchema
let method = (findMethod types "UpdateItem").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Task<_>>
// Return type must not be unit — should be the UpdateItem provided type
method.ReturnType.GetGenericArguments()[0]
|> shouldNotEqual typeof<unit>
// ── Header parameters ─────────────────────────────────────────────────────────
let private headerParamSchema =
"""openapi: "3.0.0"
info:
title: HeaderParamTest
version: "1.0.0"
paths:
/items:
get:
operationId: listItems
parameters:
- name: X-Api-Version
in: header
required: true
schema:
type: string
- name: limit
in: query
required: false
schema:
type: integer
responses:
"200":
description: OK
components:
schemas: {}
"""
[<Fact>]
let ``header parameter is included as a method parameter``() =
let types = compileTaskSchema headerParamSchema
let method = (findMethod types "ListItems").Value
let parameters = method.GetParameters()
// xApiVersion (required string) + limit (optional int) + cancellationToken
parameters.Length |> shouldEqual 3
// Header param names are camelCased
let paramNames = parameters |> Array.map(fun p -> p.Name)
paramNames |> shouldContain "xApiVersion"
[<Fact>]
let ``required header parameter is not optional``() =
let types = compileTaskSchema headerParamSchema
let method = (findMethod types "ListItems").Value
let parameters = method.GetParameters()
let headerParam = parameters |> Array.find(fun p -> p.Name = "xApiVersion")
headerParam.IsOptional |> shouldEqual false
headerParam.ParameterType |> shouldEqual typeof<string>
// ── Cookie parameters ──────────────────────────────────────────────────────────
let private cookieParamSchema =
"""openapi: "3.0.0"
info:
title: CookieParamTest
version: "1.0.0"
paths:
/session:
get:
operationId: getSession
parameters:
- name: sessionId
in: cookie
required: true
schema:
type: string
- name: theme
in: cookie
required: false
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``cookie parameter is included as a method parameter``() =
let types = compileTaskSchema cookieParamSchema
let method = (findMethod types "GetSession").Value
let parameters = method.GetParameters()
// sessionId (required string) + theme (optional string) + cancellationToken
parameters.Length |> shouldEqual 3
let paramNames = parameters |> Array.map(fun p -> p.Name)
paramNames |> shouldContain "sessionId"
paramNames |> shouldContain "theme"
[<Fact>]
let ``required cookie parameter is not optional``() =
let types = compileTaskSchema cookieParamSchema
let method = (findMethod types "GetSession").Value
let parameters = method.GetParameters()
let cookieParam = parameters |> Array.find(fun p -> p.Name = "sessionId")
cookieParam.IsOptional |> shouldEqual false
cookieParam.ParameterType |> shouldEqual typeof<string>
[<Fact>]
let ``optional cookie parameter is optional``() =
let types = compileTaskSchema cookieParamSchema
let method = (findMethod types "GetSession").Value
let parameters = method.GetParameters()
let themeParam = parameters |> Array.find(fun p -> p.Name = "theme")
themeParam.IsOptional |> shouldEqual true
// ── text/plain response ────────────────────────────────────────────────────────
let private textPlainResponseSchema =
"""openapi: "3.0.0"
info:
title: TextPlainTest
version: "1.0.0"
paths:
/health:
get:
operationId: getHealth
responses:
"200":
description: OK
content:
text/plain:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``text/plain response produces Task<string> return type``() =
let types = compileTaskSchema textPlainResponseSchema
let method = (findMethod types "GetHealth").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Task<_>>
method.ReturnType.GetGenericArguments()[0]
|> shouldEqual typeof<string>
[<Fact>]
let ``text/plain response in async mode produces Async<string> return type``() =
let types = compileAsyncSchema textPlainResponseSchema
let method = (findMethod types "GetHealth").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Async<_>>
method.ReturnType.GetGenericArguments()[0]
|> shouldEqual typeof<string>
// ── default response ───────────────────────────────────────────────────────────
let private defaultResponseSchema =
"""openapi: "3.0.0"
info:
title: DefaultResponseTest
version: "1.0.0"
paths:
/data:
get:
operationId: getData
responses:
default:
description: Default response
content:
application/json:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``default response is used as return type when no 2xx response is defined``() =
let types = compileTaskSchema defaultResponseSchema
let method = (findMethod types "GetData").Value
method.ReturnType.IsGenericType |> shouldEqual true
method.ReturnType.GetGenericTypeDefinition()
|> shouldEqual typedefof<Task<_>>
// The string schema from the default response should produce Task<string>
method.ReturnType.GetGenericArguments()[0]
|> shouldEqual typeof<string>
// ── Multiple path parameters ───────────────────────────────────────────────────
let private multiplePathParamsSchema =
"""openapi: "3.0.0"
info:
title: MultiplePathParamsTest
version: "1.0.0"
paths:
/users/{userId}/posts/{postId}:
get:
operationId: getUserPost
parameters:
- name: userId
in: path
required: true
schema:
type: integer
- name: postId
in: path
required: true
schema:
type: integer
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``both path parameters appear as required parameters``() =
let types = compileTaskSchema multiplePathParamsSchema
let method = (findMethod types "GetUserPost").Value
let parameters = method.GetParameters()
let paramNames = parameters |> Array.map(fun p -> p.Name)
paramNames |> shouldContain "userId"
paramNames |> shouldContain "postId"
[<Fact>]
let ``path parameters in nested path are required (not optional)``() =
let types = compileTaskSchema multiplePathParamsSchema
let method = (findMethod types "GetUserPost").Value
let parameters = method.GetParameters()
let userIdParam = parameters |> Array.find(fun p -> p.Name = "userId")
userIdParam.IsOptional |> shouldEqual false
let postIdParam = parameters |> Array.find(fun p -> p.Name = "postId")
postIdParam.IsOptional |> shouldEqual false
[<Fact>]
let ``multiple path params appear before CancellationToken``() =
let types = compileTaskSchema multiplePathParamsSchema
let method = (findMethod types "GetUserPost").Value
let parameters = method.GetParameters()
let lastParam = parameters |> Array.last
lastParam.ParameterType |> shouldEqual typeof<CancellationToken>
parameters.Length |> shouldEqual 3 // userId, postId, CancellationToken
[<Fact>]
let ``invokeCode for multiple path params does not reuse the same quotation Var binding``() =
let types = compileTaskSchema multiplePathParamsSchema
let method = (findMethod types "GetUserPost").Value
let invokeCode = getInvokeCode method
let thisExpr = Expr.Var(Var("this", method.DeclaringType))
let userIdExpr = Expr.Var(Var("userId", typeof<int32>))
let postIdExpr = Expr.Var(Var("postId", typeof<int32>))
let ctExpr = Expr.Var(Var("cancellationToken", typeof<CancellationToken>))
let body = invokeCode [ thisExpr; userIdExpr; postIdExpr; ctExpr ]
body
|> letBoundVars
|> containsDuplicateVarObject
|> shouldEqual false
let private multipleQueryParamsSchema =
"""openapi: "3.0.0"
info:
title: MultipleQueryParamsTest
version: "1.0.0"
paths:
/search:
get:
operationId: searchItems
parameters:
- name: q
in: query
required: true
schema:
type: string
- name: page
in: query
required: true
schema:
type: integer
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``invokeCode for multiple query params does not reuse the same quotation Var binding``() =
let types = compileTaskSchema multipleQueryParamsSchema
let method = (findMethod types "SearchItems").Value
let invokeCode = getInvokeCode method
let thisExpr = Expr.Var(Var("this", method.DeclaringType))
let qExpr = Expr.Var(Var("q", typeof<string>))
let pageExpr = Expr.Var(Var("page", typeof<int32>))
let ctExpr = Expr.Var(Var("cancellationToken", typeof<CancellationToken>))
let body = invokeCode [ thisExpr; qExpr; pageExpr; ctExpr ]
body
|> letBoundVars
|> containsDuplicateVarObject
|> shouldEqual false
// ── PATCH operation ────────────────────────────────────────────────────────────
let private patchSchema =
"""openapi: "3.0.0"
info:
title: PatchTest
version: "1.0.0"
paths:
/items/{id}:
patch:
operationId: updateItem
parameters:
- name: id
in: path
required: true
schema:
type: integer
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
responses:
"200":
description: OK
components:
schemas: {}
"""
[<Fact>]
let ``PATCH endpoint generates a method``() =
let types = compileTaskSchema patchSchema
let method = findMethod types "UpdateItem"
method.IsSome |> shouldEqual true
[<Fact>]
let ``PATCH endpoint has path param, body param, and CancellationToken``() =
let types = compileTaskSchema patchSchema
let method = (findMethod types "UpdateItem").Value
let parameters = method.GetParameters()
let paramNames = parameters |> Array.map(fun p -> p.Name)
paramNames |> shouldContain "id"
paramNames |> shouldContain "json"
let lastParam = parameters |> Array.last
lastParam.ParameterType |> shouldEqual typeof<CancellationToken>
// ── Auto-generated operation name (no operationId) ─────────────────────────────
let private noOperationIdSchema =
"""openapi: "3.0.0"
info:
title: NoOperationIdTest
version: "1.0.0"
paths:
/categories/{categoryId}/items:
get:
parameters:
- name: categoryId
in: path
required: true
schema:
type: integer
responses:
"200":
description: OK
content:
application/json:
schema:
type: string
components:
schemas: {}
"""
[<Fact>]
let ``operation without operationId generates a method from path and HTTP method``() =
let types = compileTaskSchema noOperationIdSchema
let method = findMethod types "GetCategoryItems"
method.IsSome |> shouldEqual true
[<Fact>]
let ``operation without operationId has correct parameter count``() =
let types = compileTaskSchema noOperationIdSchema
// findMethod searches all methods on all types; just verify we find exactly one
// method that has the expected signature (categoryId + CancellationToken)
let allMethods =
types
|> List.collect(fun t -> t.GetMethods() |> Array.toList)
|> List.filter(fun m ->
let ps = m.GetParameters()
ps.Length = 2
&& ps[0].Name = "categoryId"
&& ps[1].ParameterType = typeof<CancellationToken>)
allMethods.Length |> shouldEqual 1
// ── application/x-www-form-urlencoded request body ────────────────────────────
let private formUrlEncodedBodySchema =
"""openapi: "3.0.0"
info:
title: FormUrlEncodedTest
version: "1.0.0"
paths:
/login:
post:
operationId: login
requestBody:
required: true
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
username:
type: string
password:
type: string
responses:
"200":
description: OK
components:
schemas: {}
"""
[<Fact>]
let ``form-urlencoded body generates a method``() =
let types = compileTaskSchema formUrlEncodedBodySchema
let method = findMethod types "Login"
method.IsSome |> shouldEqual true
[<Fact>]
let ``form-urlencoded body parameter is named formUrlEncoded``() =
let types = compileTaskSchema formUrlEncodedBodySchema
let method = (findMethod types "Login").Value
let parameters = method.GetParameters()
let paramNames = parameters |> Array.map(fun p -> p.Name)
paramNames |> shouldContain "formUrlEncoded"
[<Fact>]
let ``form-urlencoded body has CancellationToken as last parameter``() =
let types = compileTaskSchema formUrlEncodedBodySchema
let method = (findMethod types "Login").Value
let lastParam = method.GetParameters() |> Array.last
lastParam.ParameterType |> shouldEqual typeof<CancellationToken>
// ── multipart/form-data request body ─────────────────────────────────────────
let private multipartFormDataBodySchema =
"""openapi: "3.0.0"
info:
title: MultipartTest
version: "1.0.0"
paths:
/upload:
post:
operationId: uploadFile
requestBody:
required: true
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: string
format: binary
responses:
"200":
description: OK
components:
schemas: {}
"""
[<Fact>]
let ``multipart/form-data body generates a method``() =
let types = compileTaskSchema multipartFormDataBodySchema
let method = findMethod types "UploadFile"
method.IsSome |> shouldEqual true
[<Fact>]
let ``multipart/form-data body parameter is named formData``() =
let types = compileTaskSchema multipartFormDataBodySchema
let method = (findMethod types "UploadFile").Value
let parameters = method.GetParameters()
let paramNames = parameters |> Array.map(fun p -> p.Name)