From 4df9e90de1874fd770b4410ad563c2f7d33a364f Mon Sep 17 00:00:00 2001 From: Ahmet Date: Tue, 23 Feb 2021 11:14:17 +0300 Subject: [PATCH 1/4] Added PropertyNameGenerator to AbpApiProxyScriptingOptions --- .../AbpApiProxyScriptingOptions.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs index e698d00416..62bcf10296 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Reflection; namespace Volo.Abp.Http.ProxyScripting.Configuration { @@ -7,9 +8,30 @@ namespace Volo.Abp.Http.ProxyScripting.Configuration { public IDictionary Generators { get; } + public Func PropertyNameGenerator { get; set; } + public AbpApiProxyScriptingOptions() { Generators = new Dictionary(); + + PropertyNameGenerator = propertyInfo => + { + var jsonPropertyNameAttribute = propertyInfo.GetSingleAttributeOrNull(true); + + if (jsonPropertyNameAttribute != null) + { + return jsonPropertyNameAttribute.Name; + } + + var jsonPropertyAttribute = propertyInfo.GetSingleAttributeOrNull(true); + + if (jsonPropertyAttribute != null) + { + return jsonPropertyAttribute.PropertyName; + } + + return null; + }; } } } \ No newline at end of file From d06c654d3e8bc50e77be6a4483c52824b0cfc2af Mon Sep 17 00:00:00 2001 From: Ahmet Date: Tue, 23 Feb 2021 11:57:10 +0300 Subject: [PATCH 2/4] Implementing JsonName generating for PropertyApiDescriptionModel --- .../Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs | 4 ++++ .../Configuration/AbpApiProxyScriptingOptions.cs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs index 786ea32b76..678798ae39 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel.DataAnnotations; using System.Reflection; +using Volo.Abp.Http.ProxyScripting.Configuration; namespace Volo.Abp.Http.Modeling { @@ -9,6 +10,8 @@ namespace Volo.Abp.Http.Modeling { public string Name { get; set; } + public string JsonName { get; set; } + public string Type { get; set; } public string TypeSimple { get; set; } @@ -21,6 +24,7 @@ namespace Volo.Abp.Http.Modeling return new PropertyApiDescriptionModel { Name = propertyInfo.Name, + JsonName = AbpApiProxyScriptingOptions.PropertyNameGenerator.Invoke(propertyInfo), Type = ApiTypeNameHelper.GetTypeName(propertyInfo.PropertyType), TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(propertyInfo.PropertyType), IsRequired = propertyInfo.IsDefined(typeof(RequiredAttribute), true) diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs index 62bcf10296..21bc6783bb 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Configuration/AbpApiProxyScriptingOptions.cs @@ -8,7 +8,7 @@ namespace Volo.Abp.Http.ProxyScripting.Configuration { public IDictionary Generators { get; } - public Func PropertyNameGenerator { get; set; } + public static Func PropertyNameGenerator { get; set; } public AbpApiProxyScriptingOptions() { From 8e72d54d3f4328cb7536e5419a0d2d2ea8fb3efe Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 24 Feb 2021 16:56:03 +0800 Subject: [PATCH 3/4] Add JsonName to ParameterApiDescriptionModel. --- .../AspNetCoreApiDescriptionModelProvider.cs | 1 + ...tCoreApiDescriptionModelProviderOptions.cs | 27 +++++++++++++++++++ .../Modeling/ParameterApiDescriptionModel.cs | 5 +++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs index 135b506e69..ef51a4a23f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs @@ -272,6 +272,7 @@ namespace Volo.Abp.AspNetCore.Mvc actionModel.AddParameter(ParameterApiDescriptionModel.Create( parameterDescription.Name, + _options.ApiParameterNameGenerator?.Invoke(parameterDescription), matchedMethodParamName, parameterDescription.Type, parameterDescription.RouteInfo?.IsOptional ?? false, diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs index 4cd607e07a..ba5041a067 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs @@ -2,6 +2,9 @@ using System.Linq; using System.Reflection; using System.Text; +using System.Text.Json.Serialization; +using Microsoft.AspNetCore.Mvc.ApiExplorer; +using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; using Volo.Abp.Application.Services; using Volo.Abp.AspNetCore.Mvc.Conventions; @@ -13,6 +16,8 @@ namespace Volo.Abp.AspNetCore.Mvc public Func ActionNameGenerator { get; set; } + public Func ApiParameterNameGenerator { get; set; } + public AspNetCoreApiDescriptionModelProviderOptions() { ControllerNameGenerator = (controllerType, setting) => @@ -52,6 +57,28 @@ namespace Volo.Abp.AspNetCore.Mvc return methodNameBuilder.ToString(); }; + + ApiParameterNameGenerator = (apiParameterDescription) => + { + if (apiParameterDescription.ModelMetadata is DefaultModelMetadata defaultModelMetadata) + { + var jsonPropertyNameAttribute = (System.Text.Json.Serialization.JsonPropertyNameAttribute) + defaultModelMetadata.Attributes.PropertyAttributes.FirstOrDefault(x => x is System.Text.Json.Serialization.JsonPropertyNameAttribute); + if (jsonPropertyNameAttribute != null) + { + return jsonPropertyNameAttribute.Name; + } + + var jsonPropertyAttribute = (Newtonsoft.Json.JsonPropertyAttribute) + defaultModelMetadata.Attributes.PropertyAttributes.FirstOrDefault(x => x is Newtonsoft.Json.JsonPropertyAttribute); + if (jsonPropertyAttribute != null) + { + return jsonPropertyAttribute.PropertyName; + } + } + + return null; + }; } } } diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs index 8bdead565e..8a12b5847a 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs @@ -10,6 +10,8 @@ namespace Volo.Abp.Http.Modeling public string Name { get; set; } + public string JsonName { get; set; } + public string Type { get; set; } public string TypeSimple { get; set; } @@ -29,11 +31,12 @@ namespace Volo.Abp.Http.Modeling } - public static ParameterApiDescriptionModel Create(string name, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null, string descriptorName = null) + public static ParameterApiDescriptionModel Create(string name, string jsonName, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null, string descriptorName = null) { return new ParameterApiDescriptionModel { Name = name, + JsonName = jsonName, NameOnMethod = nameOnMethod, Type = type != null ? TypeHelper.GetFullNameHandlingNullableAndGenerics(type) : null, TypeSimple = type != null ? ApiTypeNameHelper.GetSimpleTypeName(type) : null, From 21014ec80792b8ef0a9a1f611bc032b0c06d7797 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 24 Feb 2021 16:59:20 +0800 Subject: [PATCH 4/4] Handle null value. --- .../Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs index ba5041a067..f807efcfdf 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProviderOptions.cs @@ -63,14 +63,14 @@ namespace Volo.Abp.AspNetCore.Mvc if (apiParameterDescription.ModelMetadata is DefaultModelMetadata defaultModelMetadata) { var jsonPropertyNameAttribute = (System.Text.Json.Serialization.JsonPropertyNameAttribute) - defaultModelMetadata.Attributes.PropertyAttributes.FirstOrDefault(x => x is System.Text.Json.Serialization.JsonPropertyNameAttribute); + defaultModelMetadata?.Attributes?.PropertyAttributes?.FirstOrDefault(x => x is System.Text.Json.Serialization.JsonPropertyNameAttribute); if (jsonPropertyNameAttribute != null) { return jsonPropertyNameAttribute.Name; } var jsonPropertyAttribute = (Newtonsoft.Json.JsonPropertyAttribute) - defaultModelMetadata.Attributes.PropertyAttributes.FirstOrDefault(x => x is Newtonsoft.Json.JsonPropertyAttribute); + defaultModelMetadata?.Attributes?.PropertyAttributes?.FirstOrDefault(x => x is Newtonsoft.Json.JsonPropertyAttribute); if (jsonPropertyAttribute != null) { return jsonPropertyAttribute.PropertyName;