Repository navigation
Expand file tree
/
Copy pathSwaggerFeatureTests.cs
More file actions
673 lines (556 loc) · 26.1 KB
/
Copy pathSwaggerFeatureTests.cs
File metadata and controls
673 lines (556 loc) · 26.1 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Api.Swagger;
using ServiceStack.Text;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[Api("Service Description")]
[Route("/swagger/{Name}", "GET", Summary = @"GET Summary", Notes = "GET Notes")]
[Route("/swagger/{Name}", "POST", Summary = @"POST Summary", Notes = "POST Notes")]
public class SwaggerFeatureRequest
{
[ApiMember(Name="Name", Description = "Name Description",
ParameterType = "path", DataType = SwaggerType.String, IsRequired = true)]
public string Name { get; set; }
}
[Api]
[Route("/swaggerGetList/{Name}", "GET")]
public class SwaggerGetListRequest : IReturn<List<SwaggerFeatureResponse>>
{
public string Name { get; set; }
}
[Api]
[Route("/swaggerGetArray/{Name}", "GET")]
public class SwaggerGetArrayRequest : IReturn<SwaggerFeatureResponse[]>
{
public string Name { get; set; }
}
[Api]
[Route("/swaggerModels/{UrlParam}", "POST")]
public class SwaggerModelsRequest : IReturn<SwaggerFeatureResponse>
{
[ApiMember(Name = "UrlParam", Description = "URL parameter",
ParameterType = "path", DataType = SwaggerType.String, IsRequired = true)]
public string UrlParam { get; set; }
[ApiMember(Name = "RequestBody", Description = "The request body",
ParameterType = "body", DataType = "SwaggerModelsRequest", IsRequired = true)]
[System.ComponentModel.Description("Name description")]
public string RequestBody { get; set; }
[System.ComponentModel.Description("NestedModel description")]
public SwaggerNestedModel NestedModel { get; set;}
public List<SwaggerNestedModel2> ListProperty { get; set; }
public SwaggerNestedModel3[] ArrayProperty { get; set; }
public string StringProperty { get; set; }
[System.ComponentModel.Description("Byte description")]
public byte ByteProperty { get; set; }
[ServiceStack.DataAnnotations.Description("Long description")]
public long LongProperty { get; set; }
public float FloatProperty { get; set; }
public double DoubleProperty { get; set; }
public decimal DecimalProperty { get; set; }
public DateTime DateProperty { get; set; }
}
public class SwaggerNestedModel
{
[System.ComponentModel.Description("NestedProperty description")]
public bool NestedProperty { get; set;}
}
public class SwaggerNestedModel2
{
[System.ComponentModel.Description("NestedProperty2 description")]
public bool NestedProperty2 { get; set;}
}
public class SwaggerNestedModel3
{
[System.ComponentModel.Description("NestedProperty3 description")]
public bool NestedProperty3 { get; set;}
}
[Api]
[Route("/swagger2/NameIsNotSetRequest", "GET")]
public class NameIsNotSetRequest
{
[ApiMember]
public string Name { get; set; }
}
[Api("test")]
[Route("/swg3/conference/count", "GET")]
public class MultipleTestRequest : IReturn<int>
{
[ApiMember]
public string Name { get; set; }
}
[Api]
[Route("/swg3/conference/{Name}/conferences", "POST")]
[Route("/swgb3/conference/{Name}/conferences", "POST")]
public class MultipleTest2Request : IReturn<object>
{
[ApiMember]
public string Name { get; set; }
}
[Api]
[Route("/swg3/conference/{Name}/conferences", "DELETE")]
public class MultipleTest3Request : IReturn<object>
{
[ApiMember]
public string Name { get; set; }
}
[Api]
[Route("/swg3/conference", "GET")]
public class MultipleTest4Request : IReturn<object>
{
[ApiMember]
public string Name { get; set; }
}
public class NullableResponse
{
[System.ComponentModel.Description("NestedProperty2 description")]
public bool NestedProperty2 { get; set; }
public int? Optional { get; set; }
}
[Api]
[Route("/swgnull", "GET")]
public class NullableInRequest : IReturn<NullableResponse>
{
[ApiMember]
public int? Position { get; set; }
[ApiMember]
public bool IsRequired { get; set; }
}
public class NullableService : Service
{
public object Get(NullableInRequest request)
{
return null;
}
}
public class SwaggerFeatureResponse
{
public bool IsSuccess { get; set; }
}
public class MultipleTestRequestService : Service
{
public object Get(MultipleTestRequest request)
{
return null;
}
public object Post(MultipleTest2Request request)
{
return null;
}
public object Delete(MultipleTest3Request request)
{
return null;
}
}
public class MultipleTest2RequestService : Service
{
public object Get(MultipleTest4Request request)
{
return null;
}
}
public class SwaggerFeatureService : Service
{
public object Get(SwaggerFeatureRequest request)
{
return new SwaggerFeatureResponse { IsSuccess = true };
}
public object Post(SwaggerFeatureRequest request)
{
return new SwaggerFeatureResponse { IsSuccess = true };
}
public object Get(NameIsNotSetRequest request)
{
return 0;
}
public object Post(SwaggerModelsRequest request)
{
return new SwaggerFeatureResponse { IsSuccess = true };
}
public object Get(SwaggerGetListRequest request)
{
return new List<SwaggerFeatureResponse> { new SwaggerFeatureResponse { IsSuccess = true } };
}
public object Get(SwaggerGetArrayRequest request)
{
return new[] { new SwaggerFeatureResponse { IsSuccess = true } };
}
}
[DataContract]
public class DataContractBaseType
{
[DataMember]
public string Zebra { get; set; }
}
[Api]
[DataContract]
[Route("/swgdatamemberorder", "GET")]
public class DataContractDerivedTypeRequest : DataContractBaseType, IReturn<DataContractDerivedTypeResponse>
{
[DataMember(Order = 0)]
public string Bird { get; set; }
[DataMember(Order = 1)]
public string Parrot { get; set; }
[DataMember]
public string Dog { get; set; }
[DataMember(Order = 3)]
public string Antelope { get; set; }
[DataMember]
public string Cat { get; set; }
[DataMember(Order = 1)]
public string Albatross { get; set; }
}
[DataContract]
public class DataContractHierarchyType : DataContractBaseType
{
[DataMember]
public string Falcon { get; set; }
}
[DataContract]
public class DataContractDerivedTypeResponse : DataContractHierarchyType
{
[DataMember(Order = 0)]
public string Bird { get; set; }
[DataMember(Order = 1)]
public string Parrot { get; set; }
[DataMember]
public string Cat { get; set; }
[DataMember]
public string Dog { get; set; }
[DataMember(Order = 3)]
public string Antelope { get; set; }
[DataMember(Order = 1)]
public string Albatross { get; set; }
[DataMember(Order = 0, Name = "Baldeagle")]
public string Eagle { get; set; }
}
public class DataMemberAttributeOrderService : Service
{
public object Get(DataContractDerivedTypeRequest request)
{
return new DataContractDerivedTypeResponse();
}
}
[TestFixture]
public class SwaggerFeatureServiceTests
{
private const string BaseUrl = "http://localhost:8024";
private const string ListeningOn = BaseUrl + "/";
public class SwaggerFeatureAppHostHttpListener
: AppHostHttpListenerBase
{
public SwaggerFeatureAppHostHttpListener()
: base("Swagger Feature Tests", typeof(SwaggerFeatureServiceTests).Assembly) { }
public override void Configure(Funq.Container container)
{
Plugins.Add(new SwaggerFeature());
SetConfig(new HostConfig
{
DebugMode = true //Show StackTraces for easier debugging
});
}
}
SwaggerFeatureAppHostHttpListener appHost;
[TestFixtureSetUp]
public void TestFixtureSetUp()
{
appHost = new SwaggerFeatureAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
appHost.Dispose();
}
static IRestClient[] RestClients =
{
new JsonServiceClient(ListeningOn)
//new XmlServiceClient(ServiceClientBaseUri),
};
[Test, Explicit]
public void RunFor5Mins()
{
appHost.LoadPlugin(new CorsFeature("http://localhost:50000"));
Debug.WriteLine(ListeningOn + "resources");
Thread.Sleep(TimeSpan.FromMinutes(5));
}
[Test, TestCaseSource("RestClients")]
public void Should_get_default_name_from_property(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swagger2/NameIsNotSetRequest");
var p = resource.Apis.SelectMany(t => t.Operations).SelectMany(t => t.Parameters);
Assert.That(p.Count(), Is.EqualTo(1));
Assert.That(p.FirstOrDefault(t=>t.Name == "Name"), Is.Not.Null);
}
[Test, TestCaseSource("RestClients")]
public void Should_group_similar_services(IRestClient client)
{
var resources = client.Get<SwaggerResourcesResponse>("/resources");
var swagger = resources.Apis.Where(t => t.Path.Contains("/resource/swg3"));
Assert.That(swagger.Count(), Is.EqualTo(1));
}
[Test, TestCaseSource("RestClients")]
public void Should_distinct_base_path(IRestClient client)
{
var resources = client.Get<SwaggerResourcesResponse>("/resources");
var swagger = resources.Apis.Where(t => t.Path.Contains("/resource/swgb3"));
Assert.That(swagger.Count(), Is.EqualTo(1));
}
[Test, TestCaseSource("RestClients")]
public void Should_list_services(IRestClient client)
{
var resources = client.Get<SwaggerResourcesResponse>("/resources");
Assert.That(resources.BasePath, Is.EqualTo(BaseUrl));
Assert.That(resources.SwaggerVersion, Is.EqualTo("1.2"));
Assert.That(resources.Apis, Is.Not.Null);
var swagger = resources.Apis.FirstOrDefault(t => t.Path == "/resource/swagger");
Assert.That(swagger, Is.Not.Null);
Assert.That(swagger.Description, Is.EqualTo("Service Description"));
}
[Test, TestCaseSource("RestClients")]
public void Should_use_webhosturl_as_resources_base_path_when_configured(IRestClient client)
{
const string webHostUrl = "https://host.example.com/_api";
try
{
appHost.Config.WebHostUrl = webHostUrl;
var resources = client.Get<SwaggerResourcesResponse>("/resources");
Assert.That(resources.BasePath, Is.EqualTo(webHostUrl));
}
finally
{
appHost.Config.WebHostUrl = null;
}
}
[Test, TestCaseSource("RestClients")]
public void Should_use_webhosturl_as_resource_base_path_when_configured(IRestClient client)
{
const string webHostUrl = "https://host.example.com/_api";
try
{
appHost.Config.WebHostUrl = webHostUrl;
var resource = client.Get<SwaggerApiDeclaration>("/resource/swagger");
Assert.That(resource.BasePath, Is.EqualTo(webHostUrl));
}
finally
{
appHost.Config.WebHostUrl = null;
}
}
[Test, TestCaseSource("RestClients")]
public void Should_use_https_for_resources_basepath_when_usehttpslinks_config_is_true(IRestClient client)
{
try
{
appHost.Config.UseHttpsLinks = true;
var resources = client.Get<SwaggerResourcesResponse>("/resources");
Assert.That(resources.BasePath.ToLowerInvariant(), Is.StringStarting("https"));
}
finally
{
appHost.Config.UseHttpsLinks = false;
}
}
[Test, TestCaseSource("RestClients")]
public void Should_use_https_for_resource_basepath_when_usehttpslinks_config_is_true(IRestClient client)
{
try
{
appHost.Config.UseHttpsLinks = true;
var resource = client.Get<SwaggerApiDeclaration>("/resource/swagger");
resource.PrintDump();
Assert.That(resource.BasePath.ToLowerInvariant(), Is.StringStarting("https"));
}
finally
{
appHost.Config.UseHttpsLinks = false;
}
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_service_parameters(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swagger");
Assert.That(resource.BasePath, Is.EqualTo(BaseUrl));
Assert.That(resource.ResourcePath, Is.EqualTo("/swagger"));
Assert.That(resource.Apis, Is.Not.Empty);
var operations = new List<SwaggerOperation>();
foreach(var api in resource.Apis) operations.AddRange(api.Operations);
var getOperation = operations.Single(t => t.Method == "GET");
Assert.That(getOperation.Summary, Is.EqualTo("GET Summary"));
Assert.That(getOperation.Notes, Is.EqualTo("GET Notes"));
Assert.That(getOperation.Method, Is.EqualTo("GET"));
Assert.That(getOperation.Parameters, Is.Not.Empty);
var p1 = getOperation.Parameters[0];
Assert.That(p1.Name, Is.EqualTo("Name"));
Assert.That(p1.Description, Is.EqualTo("Name Description"));
Assert.That(p1.Type, Is.EqualTo("string"));
Assert.That(p1.ParamType, Is.EqualTo("path"));
Assert.That(p1.Required, Is.EqualTo(true));
var postOperation = operations.Single(t => t.Method == "POST");
Assert.That(postOperation.Summary, Is.EqualTo("POST Summary"));
Assert.That(postOperation.Notes, Is.EqualTo("POST Notes"));
Assert.That(postOperation.Method, Is.EqualTo("POST"));
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_response_class_name(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerModels");
Assert.That(resource.Apis, Is.Not.Empty);
var postOperation = resource.Apis.SelectMany(api => api.Operations).Single(t => t.Method == "POST");
Assert.That(postOperation.ResponseClass, Is.EqualTo(typeof(SwaggerFeatureResponse).Name));
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_list_response_type_info(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerGetList");
Assert.That(resource.Apis, Is.Not.Empty);
var operation = resource.Apis.SelectMany(api => api.Operations).Single(t => t.Method == "GET");
Assert.That(operation.ResponseClass, Is.EqualTo("List[SwaggerFeatureResponse]"));
Assert.That(resource.Models.ContainsKey("SwaggerFeatureResponse"));
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_array_response_type_info(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerGetArray");
Assert.That(resource.Apis, Is.Not.Empty);
var operation = resource.Apis.SelectMany(api => api.Operations).Single(t => t.Method == "GET");
Assert.That(operation.ResponseClass, Is.EqualTo("List[SwaggerFeatureResponse]"));
Assert.That(resource.Models.ContainsKey("SwaggerFeatureResponse"));
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_response_model(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerModels");
Assert.That(resource.Models, Is.Not.Empty);
Assert.That(resource.Models.ContainsKey(typeof(SwaggerFeatureResponse).Name), Is.True);
var responseClassModel = resource.Models[typeof(SwaggerFeatureResponse).Name];
Assert.That(responseClassModel.Id, Is.EqualTo(typeof(SwaggerFeatureResponse).Name));
Assert.That(responseClassModel.Properties, Is.Not.Empty);
Assert.That(responseClassModel.Properties.ContainsKey("IsSuccess"), Is.True);
Assert.That(responseClassModel.Properties["IsSuccess"].Type, Is.EqualTo(SwaggerType.Boolean));
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_request_body_model(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerModels");
Assert.That(resource.Models, Is.Not.Empty);
var key = typeof(SwaggerModelsRequest).Name;
Assert.That(resource.Models.ContainsKey(key), Is.True);
var requestClassModel = resource.Models[key];
Assert.That(requestClassModel.Id, Is.EqualTo(key));
Assert.That(requestClassModel.Properties, Is.Not.Empty);
Assert.That(requestClassModel.Properties.ContainsKey("RequestBody"), Is.True);
Assert.That(requestClassModel.Properties["RequestBody"].Type, Is.EqualTo("SwaggerModelsRequest"));
Assert.That(requestClassModel.Properties["RequestBody"].Description, Is.EqualTo("The request body"));
Assert.That(requestClassModel.Properties.ContainsKey("StringProperty"), Is.True);
Assert.That(requestClassModel.Properties["StringProperty"].Type, Is.EqualTo(SwaggerType.String));
Assert.That(requestClassModel.Properties.ContainsKey("ByteProperty"));
Assert.That(requestClassModel.Properties["ByteProperty"].Type, Is.EqualTo(SwaggerType.Byte));
Assert.That(requestClassModel.Properties["ByteProperty"].Description, Is.EqualTo("Byte description"));
Assert.That(resource.Models.ContainsKey(typeof(byte).Name), Is.False);
Assert.That(requestClassModel.Properties.ContainsKey("LongProperty"));
Assert.That(requestClassModel.Properties["LongProperty"].Type, Is.EqualTo(SwaggerType.Long));
Assert.That(requestClassModel.Properties["LongProperty"].Description, Is.EqualTo("Long description"));
Assert.That(resource.Models.ContainsKey(typeof(long).Name), Is.False);
Assert.That(requestClassModel.Properties.ContainsKey("FloatProperty"));
Assert.That(requestClassModel.Properties["FloatProperty"].Type, Is.EqualTo(SwaggerType.Float));
Assert.That(resource.Models.ContainsKey(typeof(float).Name), Is.False);
Assert.That(requestClassModel.Properties.ContainsKey("DoubleProperty"));
Assert.That(requestClassModel.Properties["DoubleProperty"].Type, Is.EqualTo(SwaggerType.Double));
Assert.That(resource.Models.ContainsKey(typeof(double).Name), Is.False);
// Swagger has no concept of a "decimal" type
Assert.That(requestClassModel.Properties.ContainsKey("DecimalProperty"));
Assert.That(requestClassModel.Properties["DecimalProperty"].Type, Is.EqualTo(SwaggerType.Double));
Assert.That(resource.Models.ContainsKey(typeof(decimal).Name), Is.False);
Assert.That(requestClassModel.Properties.ContainsKey("DateProperty"));
Assert.That(requestClassModel.Properties["DateProperty"].Type, Is.EqualTo(SwaggerType.Date));
Assert.That(resource.Models.ContainsKey(typeof(DateTime).Name), Is.False);
key = typeof(SwaggerNestedModel).Name;
Assert.That(requestClassModel.Properties.ContainsKey("NestedModel"), Is.True);
Assert.That(requestClassModel.Properties["NestedModel"].Type, Is.EqualTo(key));
Assert.That(requestClassModel.Properties["NestedModel"].Description, Is.EqualTo("NestedModel description"));
Assert.That(resource.Models.ContainsKey(key), Is.True);
var nestedClassModel = resource.Models[key];
Assert.That(nestedClassModel.Properties.ContainsKey("NestedProperty"), Is.True);
Assert.That(nestedClassModel.Properties["NestedProperty"].Type, Is.EqualTo(SwaggerType.Boolean));
Assert.That(nestedClassModel.Properties["NestedProperty"].Description, Is.EqualTo("NestedProperty description"));
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_list_property_model(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerModels");
var key = typeof(SwaggerModelsRequest).Name;
Assert.That(resource.Models.ContainsKey(key), Is.True);
var requestClassModel = resource.Models[key];
Assert.That(requestClassModel.Properties.ContainsKey("ListProperty"), Is.True);
Assert.That(requestClassModel.Properties["ListProperty"].Type, Is.EqualTo(SwaggerType.Array));
key = typeof(SwaggerNestedModel2).Name;
Assert.That(requestClassModel.Properties["ListProperty"].Items["$ref"], Is.EqualTo(key));
Assert.That(resource.Models.ContainsKey(key), Is.True);
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_array_property_model(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swaggerModels");
var key = typeof(SwaggerModelsRequest).Name;
Assert.That(resource.Models.ContainsKey(key), Is.True);
var requestClassModel = resource.Models[key];
Assert.That(requestClassModel.Properties.ContainsKey("ArrayProperty"), Is.True);
Assert.That(requestClassModel.Properties["ArrayProperty"].Type, Is.EqualTo(SwaggerType.Array));
key = typeof(SwaggerNestedModel3).Name;
Assert.That(requestClassModel.Properties["ArrayProperty"].Items["$ref"], Is.EqualTo(key));
Assert.That(resource.Models.ContainsKey(key), Is.True);
}
[Test, TestCaseSource("RestClients")]
public void Should_retrieve_valid_nullable_fields(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swgnull");
var key = typeof(NullableInRequest).Name;
Assert.That(resource.Models.ContainsKey(key), Is.True);
var requestClassModel = resource.Models[key];
Assert.That(requestClassModel.Properties.ContainsKey("Position"), Is.True);
Assert.That(requestClassModel.Properties["Position"].Type, Is.EqualTo(SwaggerType.Int));
Assert.That(resource.Models.ContainsKey(typeof(NullableResponse).Name), Is.True);
Assert.That(!requestClassModel.Required.Contains("Position"));
Assert.That(requestClassModel.Required.Contains("IsRequired"));
var responseModel = resource.Models[typeof(NullableResponse).Name];
Assert.That(responseModel.Properties.ContainsKey("Optional"), Is.True);
Assert.That(!responseModel.Required.Contains("Optional"));
Assert.That(responseModel.Properties["Optional"].Type, Is.EqualTo(SwaggerType.Int));
Assert.That(responseModel.Required.Contains("NestedProperty2"));
}
// Ordering defined by: http://msdn.microsoft.com/en-us/library/ms729813.aspx
[Test, TestCaseSource("RestClients")]
public void Should_order_fields_with_DataMemberAttribute(IRestClient client)
{
var resource = client.Get<SwaggerApiDeclaration>("/resource/swgdatamemberorder");
var key = typeof(DataContractDerivedTypeRequest).Name;
Assert.That(resource.Models.ContainsKey(key), Is.True);
var requestClassModel = resource.Models[key];
Assert.That(requestClassModel.Properties.ElementAt(0).Key, Is.EqualTo("Zebra"));
Assert.That(requestClassModel.Properties.ElementAt(1).Key, Is.EqualTo("Cat"));
Assert.That(requestClassModel.Properties.ElementAt(2).Key, Is.EqualTo("Dog"));
Assert.That(requestClassModel.Properties.ElementAt(3).Key, Is.EqualTo("Bird"));
Assert.That(requestClassModel.Properties.ElementAt(4).Key, Is.EqualTo("Albatross"));
Assert.That(requestClassModel.Properties.ElementAt(5).Key, Is.EqualTo("Parrot"));
Assert.That(requestClassModel.Properties.ElementAt(6).Key, Is.EqualTo("Antelope"));
Assert.That(resource.Models.ContainsKey(typeof(DataContractDerivedTypeResponse).Name), Is.True);
var responseClassModel = resource.Models[typeof(DataContractDerivedTypeResponse).Name];
Assert.That(responseClassModel.Properties.ElementAt(0).Key, Is.EqualTo("Zebra"));
Assert.That(responseClassModel.Properties.ElementAt(1).Key, Is.EqualTo("Falcon"));
Assert.That(responseClassModel.Properties.ElementAt(2).Key, Is.EqualTo("Cat"));
Assert.That(responseClassModel.Properties.ElementAt(3).Key, Is.EqualTo("Dog"));
Assert.That(responseClassModel.Properties.ElementAt(4).Key, Is.EqualTo("Baldeagle"));
Assert.That(responseClassModel.Properties.ElementAt(5).Key, Is.EqualTo("Bird"));
Assert.That(responseClassModel.Properties.ElementAt(6).Key, Is.EqualTo("Albatross"));
Assert.That(responseClassModel.Properties.ElementAt(7).Key, Is.EqualTo("Parrot"));
Assert.That(responseClassModel.Properties.ElementAt(8).Key, Is.EqualTo("Antelope"));
}
}
}