From 73442d2700b51db3f68ea3ef8d574aed5410127f Mon Sep 17 00:00:00 2001 From: warren-spawtz <41991703+warren-spawtz@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:39:28 +0000 Subject: [PATCH 1/2] Parse FromBody types --- .../Implementations/SwaggerDocumentationTools.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ApiDocumentation/Implementations/SwaggerDocumentationTools.cs b/ApiDocumentation/Implementations/SwaggerDocumentationTools.cs index 7c3e442..f6b5bae 100644 --- a/ApiDocumentation/Implementations/SwaggerDocumentationTools.cs +++ b/ApiDocumentation/Implementations/SwaggerDocumentationTools.cs @@ -88,9 +88,13 @@ public Dictionary GetControllerModels( Type controllerType foreach ( var apiDocumentationAttributesAndReturnType in GetApiDocumentationAttributesAndReturnTypes( controllerType ) ) { result.Merge( _modelsGenerator.GetModels( apiDocumentationAttributesAndReturnType.Key.ReturnType ?? apiDocumentationAttributesAndReturnType.Value ) ); - } - return result; + //Get models for parameters + if ( apiDocumentationAttributesAndReturnType.Key.FormBody != null ) + result.Merge( _modelsGenerator.GetModels(apiDocumentationAttributesAndReturnType.Key.FormBody) ); + } + + return result; } private IEnumerable GetControllerMethods( Type controllerType ) From 4866e8d8ffe1a643b5e96e83aab418a84b8046ca Mon Sep 17 00:00:00 2001 From: warren-spawtz <41991703+warren-spawtz@users.noreply.github.com> Date: Fri, 20 Dec 2024 15:32:51 +0000 Subject: [PATCH 2/2] Handle nullable types gracefully --- .../Implementations/ModelsGenerator.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ApiDocumentation/Implementations/ModelsGenerator.cs b/ApiDocumentation/Implementations/ModelsGenerator.cs index 57355b3..e15b5ba 100644 --- a/ApiDocumentation/Implementations/ModelsGenerator.cs +++ b/ApiDocumentation/Implementations/ModelsGenerator.cs @@ -40,7 +40,10 @@ public virtual Dictionary GetModels( Type type ) if ( IsArray( type ) ) type = type.GetElementType(); - var name = _typeToStringConverter.GetApiOperationType( type ); + if (IsNullableType(type)) + type = Nullable.GetUnderlyingType(type); + + var name = _typeToStringConverter.GetApiOperationType( type ); var apiDocModels = InitializeApiDocModels( name ); ProcessType( type, apiDocModels ); @@ -52,7 +55,12 @@ private static bool IsTask(Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>); - private void ProcessType( Type type, Dictionary apiDocModels ) + private static bool IsNullableType(Type typeToConvert) + { + return Nullable.GetUnderlyingType(typeToConvert) != null; + } + + private void ProcessType( Type type, Dictionary apiDocModels ) { foreach ( var property in type.GetProperties() ) { @@ -81,8 +89,12 @@ private void ProcessType( Type type, Dictionary apiDocModel modelProperty = GetArrayModelProperty( typeof( KeyValuePair<,> ) ); } + else if (IsNullableType(propertyType)) + { + propertyType = Nullable.GetUnderlyingType(propertyType); + } - GetNonPrimitiveModels( propertyType, apiDocModels ); + GetNonPrimitiveModels( propertyType, apiDocModels ); apiDocModels.First().Value.properties.Add( property.Name, modelProperty ); if ( property.GetCustomAttributes( typeof( OptionalAttribute ), true ).Length == 0 ) apiDocModels.First().Value.required.Add( property.Name );