@@ -20,7 +20,7 @@ public class SwaggerDefinitionBuilder
2020 readonly HttpContext httpContext ;
2121 readonly IEnumerable < MethodInfo > handlers ;
2222
23- ILookup < Tuple < string , string > , XmlCommentStructure > xDocLookup ;
23+ ILookup < Tuple < string , string > , XmlCommentStructure > ? xDocLookup ;
2424
2525 public SwaggerDefinitionBuilder ( SwaggerOptions options , HttpContext httpContext , IEnumerable < MethodInfo > handlers )
2626 {
@@ -33,15 +33,13 @@ public byte[] BuildSwaggerJson()
3333 {
3434 try
3535 {
36- if ( options . XmlDocumentPath != null && ! File . Exists ( options . XmlDocumentPath ) )
36+ if ( options . XmlDocumentPath != null && File . Exists ( options . XmlDocumentPath ) )
3737 {
38- xDocLookup = null ;
38+ xDocLookup = BuildXmlMemberCommentStructure ( options . XmlDocumentPath ) ;
3939 }
4040 else
4141 {
42- xDocLookup = ( options . XmlDocumentPath != null )
43- ? BuildXmlMemberCommentStructure ( options . XmlDocumentPath )
44- : null ;
42+ xDocLookup = null ;
4543 }
4644
4745 var doc = new SwaggerDocument ( ) ;
@@ -54,7 +52,7 @@ public byte[] BuildSwaggerJson()
5452
5553 // tags.
5654 var xmlServiceName = ( xDocLookup != null )
57- ? BuildXmlTypeSummary ( options . XmlDocumentPath )
55+ ? BuildXmlTypeSummary ( options . XmlDocumentPath ! ) // xDocLookup is not null if XmlDocumentPath is not null.
5856 : null ;
5957
6058 doc . tags = handlers
@@ -63,7 +61,7 @@ public byte[] BuildSwaggerJson()
6361 . Distinct ( )
6462 . Select ( x =>
6563 {
66- string desc = null ;
64+ string ? desc = null ;
6765 if ( xmlServiceName != null )
6866 {
6967 xmlServiceName . TryGetValue ( x , out desc ) ;
@@ -80,7 +78,7 @@ public byte[] BuildSwaggerJson()
8078 {
8179 // MemberInfo.DeclaringType is null only if it is a member of a VB Module.
8280 string declaringTypeName = item . DeclaringType ! . Name ;
83- XmlCommentStructure xmlComment = null ;
81+ XmlCommentStructure ? xmlComment = null ;
8482 if ( xDocLookup != null )
8583 {
8684 // ParameterInfo.Name will be null only it is ReturnParameter.
@@ -123,7 +121,7 @@ public byte[] BuildSwaggerJson()
123121 }
124122 }
125123
126- Schemas . Parameter [ ] BuildParameters ( IDictionary < string , Schema > definitions , XmlCommentStructure xmlComment , MethodInfo method )
124+ Schemas . Parameter [ ] BuildParameters ( IDictionary < string , Schema > definitions , XmlCommentStructure ? xmlComment , MethodInfo method )
127125 {
128126 var parameterInfos = method . GetParameters ( ) ;
129127 var parameters = parameterInfos
@@ -132,7 +130,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
132130 var parameterXmlComment = UnwrapTypeName ( x . ParameterType ) ;
133131 if ( xmlComment != null )
134132 {
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 ! ) ;
136135 parameterXmlComment = UnwrapTypeName ( x . ParameterType ) + " " + parameterXmlComment ;
137136 }
138137
@@ -147,8 +146,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
147146 ? new PartialSchema { type = ToSwaggerDataType ( collectionType ) }
148147 : null ;
149148
150- string defaultObjectExample = null ;
151- object [ ] enums = null ;
149+ string ? defaultObjectExample = null ;
150+ object [ ] ? enums = null ;
152151 if ( x . ParameterType . GetTypeInfo ( ) . IsEnum || ( collectionType != null && collectionType . GetTypeInfo ( ) . IsEnum ) )
153152 {
154153 // Compiler cannot understand collectionType is not null.
@@ -169,7 +168,7 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
169168 }
170169
171170 var swaggerDataType = ToSwaggerDataType ( x . ParameterType ) ;
172- Schema refSchema = null ;
171+ Schema ? refSchema = null ;
173172 if ( swaggerDataType == "object" )
174173 {
175174 BuildSchema ( definitions , x . ParameterType ) ;
@@ -203,7 +202,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
203202 var fullName = type . FullName ;
204203 if ( fullName == null ) return "" ; // safety(TODO:IDictionary<> is not supported)
205204
206- Schema schema ;
205+ Schema ? schema ;
207206 if ( definitions . TryGetValue ( fullName , out schema ) ) return "#/definitions/" + fullName ;
208207
209208 var properties = type . GetProperties ( BindingFlags . Instance | BindingFlags . Public ) ;
@@ -229,10 +228,11 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
229228 }
230229 else
231230 {
232- Schema items = null ;
231+ Schema ? items = null ;
233232 if ( swaggerDataType == "array" )
234233 {
235- var collectionType = GetCollectionType ( memberType ) ;
234+ // If swaggerDataType is array, it will be Collection.
235+ Type collectionType = GetCollectionType ( memberType ) ! ;
236236 var dataType = ToSwaggerDataType ( collectionType ) ;
237237 if ( dataType == "object" )
238238 {
@@ -258,7 +258,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
258258 }
259259 }
260260
261- IList < object > schemaEnum = null ;
261+ IList < object > ? schemaEnum = null ;
262262 if ( memberType . GetTypeInfo ( ) . IsEnum )
263263 {
264264 schemaEnum = Enum . GetNames ( memberType ) ;
@@ -298,7 +298,7 @@ static Type GetMemberType(MemberInfo memberInfo)
298298 throw new Exception ( ) ;
299299 }
300300
301- static Type GetCollectionType ( Type type )
301+ static Type ? GetCollectionType ( Type type )
302302 {
303303 if ( type . IsArray ) return type . GetElementType ( ) ;
304304
@@ -338,14 +338,14 @@ static ILookup<Tuple<string, string>, XmlCommentStructure> BuildXmlMemberComment
338338 . ToDictionary ( e => e . Item1 , e => e . Item2 . Value . Trim ( ) ) ;
339339
340340 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+ ) ;
349349 } )
350350 . ToLookup ( x => Tuple . Create ( x . ClassName , x . MethodName ) ) ;
351351
@@ -422,7 +422,7 @@ static string UnwrapTypeName(Type t)
422422 return Regex . Replace ( t . GetGenericTypeDefinition ( ) . Name , @"`.+$" , "" ) + "<" + innerFormat + ">" ;
423423 }
424424
425- class Item1EqualityCompaerer < T1 , T2 > : EqualityComparer < Tuple < T1 , T2 > >
425+ class Item1EqualityCompaerer < T1 , T2 > : EqualityComparer < Tuple < T1 , T2 > > where T1 : class
426426 {
427427 public override bool Equals ( Tuple < T1 , T2 > x , Tuple < T1 , T2 > y )
428428 {
@@ -443,6 +443,16 @@ class XmlCommentStructure
443443 public string Remarks { get ; set ; }
444444 public Dictionary < string , string > Parameters { get ; set ; }
445445 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+ }
446456 }
447457 }
448458
@@ -459,7 +469,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
459469 {
460470 property . ShouldSerialize = instance =>
461471 {
462- IEnumerable enumerable = null ;
472+ IEnumerable ? enumerable = null ;
463473
464474 // this value could be in a public field or public property
465475 switch ( member . MemberType )
0 commit comments