@@ -20,7 +20,7 @@ public class SwaggerDefinitionBuilder
20
20
readonly HttpContext httpContext ;
21
21
readonly IEnumerable < MethodInfo > handlers ;
22
22
23
- ILookup < Tuple < string , string > , XmlCommentStructure > xDocLookup ;
23
+ ILookup < Tuple < string , string > , XmlCommentStructure > ? xDocLookup ;
24
24
25
25
public SwaggerDefinitionBuilder ( SwaggerOptions options , HttpContext httpContext , IEnumerable < MethodInfo > handlers )
26
26
{
@@ -33,15 +33,13 @@ public byte[] BuildSwaggerJson()
33
33
{
34
34
try
35
35
{
36
- if ( options . XmlDocumentPath != null && ! File . Exists ( options . XmlDocumentPath ) )
36
+ if ( options . XmlDocumentPath != null && File . Exists ( options . XmlDocumentPath ) )
37
37
{
38
- xDocLookup = null ;
38
+ xDocLookup = BuildXmlMemberCommentStructure ( options . XmlDocumentPath ) ;
39
39
}
40
40
else
41
41
{
42
- xDocLookup = ( options . XmlDocumentPath != null )
43
- ? BuildXmlMemberCommentStructure ( options . XmlDocumentPath )
44
- : null ;
42
+ xDocLookup = null ;
45
43
}
46
44
47
45
var doc = new SwaggerDocument ( ) ;
@@ -54,7 +52,7 @@ public byte[] BuildSwaggerJson()
54
52
55
53
// tags.
56
54
var xmlServiceName = ( xDocLookup != null )
57
- ? BuildXmlTypeSummary ( options . XmlDocumentPath )
55
+ ? BuildXmlTypeSummary ( options . XmlDocumentPath ! ) // xDocLookup is not null if XmlDocumentPath is not null.
58
56
: null ;
59
57
60
58
doc . tags = handlers
@@ -63,7 +61,7 @@ public byte[] BuildSwaggerJson()
63
61
. Distinct ( )
64
62
. Select ( x =>
65
63
{
66
- string desc = null ;
64
+ string ? desc = null ;
67
65
if ( xmlServiceName != null )
68
66
{
69
67
xmlServiceName . TryGetValue ( x , out desc ) ;
@@ -80,7 +78,7 @@ public byte[] BuildSwaggerJson()
80
78
{
81
79
// MemberInfo.DeclaringType is null only if it is a member of a VB Module.
82
80
string declaringTypeName = item . DeclaringType ! . Name ;
83
- XmlCommentStructure xmlComment = null ;
81
+ XmlCommentStructure ? xmlComment = null ;
84
82
if ( xDocLookup != null )
85
83
{
86
84
// ParameterInfo.Name will be null only it is ReturnParameter.
@@ -123,7 +121,7 @@ public byte[] BuildSwaggerJson()
123
121
}
124
122
}
125
123
126
- Schemas . Parameter [ ] BuildParameters ( IDictionary < string , Schema > definitions , XmlCommentStructure xmlComment , MethodInfo method )
124
+ Schemas . Parameter [ ] BuildParameters ( IDictionary < string , Schema > definitions , XmlCommentStructure ? xmlComment , MethodInfo method )
127
125
{
128
126
var parameterInfos = method . GetParameters ( ) ;
129
127
var parameters = parameterInfos
@@ -132,7 +130,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
132
130
var parameterXmlComment = UnwrapTypeName ( x . ParameterType ) ;
133
131
if ( xmlComment != null )
134
132
{
135
- xmlComment . Parameters . TryGetValue ( x . Name , out parameterXmlComment ! ) ;
133
+ // Name is null only if Parameter is ReturnParameter.
134
+ xmlComment . Parameters . TryGetValue ( x . Name ! , out parameterXmlComment ! ) ;
136
135
parameterXmlComment = UnwrapTypeName ( x . ParameterType ) + " " + parameterXmlComment ;
137
136
}
138
137
@@ -147,8 +146,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
147
146
? new PartialSchema { type = ToSwaggerDataType ( collectionType ) }
148
147
: null ;
149
148
150
- string defaultObjectExample = null ;
151
- object [ ] enums = null ;
149
+ string ? defaultObjectExample = null ;
150
+ object [ ] ? enums = null ;
152
151
if ( x . ParameterType . GetTypeInfo ( ) . IsEnum || ( collectionType != null && collectionType . GetTypeInfo ( ) . IsEnum ) )
153
152
{
154
153
// Compiler cannot understand collectionType is not null.
@@ -169,7 +168,7 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
169
168
}
170
169
171
170
var swaggerDataType = ToSwaggerDataType ( x . ParameterType ) ;
172
- Schema refSchema = null ;
171
+ Schema ? refSchema = null ;
173
172
if ( swaggerDataType == "object" )
174
173
{
175
174
BuildSchema ( definitions , x . ParameterType ) ;
@@ -203,7 +202,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
203
202
var fullName = type . FullName ;
204
203
if ( fullName == null ) return "" ; // safety(TODO:IDictionary<> is not supported)
205
204
206
- Schema schema ;
205
+ Schema ? schema ;
207
206
if ( definitions . TryGetValue ( fullName , out schema ) ) return "#/definitions/" + fullName ;
208
207
209
208
var properties = type . GetProperties ( BindingFlags . Instance | BindingFlags . Public ) ;
@@ -229,10 +228,11 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
229
228
}
230
229
else
231
230
{
232
- Schema items = null ;
231
+ Schema ? items = null ;
233
232
if ( swaggerDataType == "array" )
234
233
{
235
- var collectionType = GetCollectionType ( memberType ) ;
234
+ // If swaggerDataType is array, it will be Collection.
235
+ Type collectionType = GetCollectionType ( memberType ) ! ;
236
236
var dataType = ToSwaggerDataType ( collectionType ) ;
237
237
if ( dataType == "object" )
238
238
{
@@ -258,7 +258,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
258
258
}
259
259
}
260
260
261
- IList < object > schemaEnum = null ;
261
+ IList < object > ? schemaEnum = null ;
262
262
if ( memberType . GetTypeInfo ( ) . IsEnum )
263
263
{
264
264
schemaEnum = Enum . GetNames ( memberType ) ;
@@ -298,7 +298,7 @@ static Type GetMemberType(MemberInfo memberInfo)
298
298
throw new Exception ( ) ;
299
299
}
300
300
301
- static Type GetCollectionType ( Type type )
301
+ static Type ? GetCollectionType ( Type type )
302
302
{
303
303
if ( type . IsArray ) return type . GetElementType ( ) ;
304
304
@@ -338,14 +338,14 @@ static ILookup<Tuple<string, string>, XmlCommentStructure> BuildXmlMemberComment
338
338
. ToDictionary ( e => e . Item1 , e => e . Item2 . Value . Trim ( ) ) ;
339
339
340
340
return new XmlCommentStructure
341
- {
342
- ClassName = match . Groups [ 1 ] . Value ,
343
- MethodName = match . Groups [ 2 ] . Value ,
344
- Summary = summary . Trim ( ) ,
345
- Remarks = remarks . Trim ( ) ,
346
- Parameters = parameters ,
347
- Returns = returns . Trim ( )
348
- } ;
341
+ (
342
+ className : match . Groups [ 1 ] . Value ,
343
+ methodName : match . Groups [ 2 ] . Value ,
344
+ summary : summary . Trim ( ) ,
345
+ remarks : remarks . Trim ( ) ,
346
+ parameters : parameters ,
347
+ returns : returns . Trim ( )
348
+ ) ;
349
349
} )
350
350
. ToLookup ( x => Tuple . Create ( x . ClassName , x . MethodName ) ) ;
351
351
@@ -422,7 +422,7 @@ static string UnwrapTypeName(Type t)
422
422
return Regex . Replace ( t . GetGenericTypeDefinition ( ) . Name , @"`.+$" , "" ) + "<" + innerFormat + ">" ;
423
423
}
424
424
425
- class Item1EqualityCompaerer < T1 , T2 > : EqualityComparer < Tuple < T1 , T2 > >
425
+ class Item1EqualityCompaerer < T1 , T2 > : EqualityComparer < Tuple < T1 , T2 > > where T1 : class
426
426
{
427
427
public override bool Equals ( Tuple < T1 , T2 > x , Tuple < T1 , T2 > y )
428
428
{
@@ -443,6 +443,16 @@ class XmlCommentStructure
443
443
public string Remarks { get ; set ; }
444
444
public Dictionary < string , string > Parameters { get ; set ; }
445
445
public string Returns { get ; set ; }
446
+
447
+ public XmlCommentStructure ( string className , string methodName , string summary , string remarks , Dictionary < string , string > parameters , string returns )
448
+ {
449
+ ClassName = className ;
450
+ MethodName = methodName ;
451
+ Summary = summary ;
452
+ Remarks = remarks ;
453
+ Parameters = parameters ;
454
+ Returns = returns ;
455
+ }
446
456
}
447
457
}
448
458
@@ -459,7 +469,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
459
469
{
460
470
property . ShouldSerialize = instance =>
461
471
{
462
- IEnumerable enumerable = null ;
472
+ IEnumerable ? enumerable = null ;
463
473
464
474
// this value could be in a public field or public property
465
475
switch ( member . MemberType )
0 commit comments