Skip to content

Opt in or Opt out

fonlow edited this page Jun 2, 2017 · 3 revisions

By default, the cherry-picking of data types is opt-in, so custom data types implemented in assemblies declared in "DataModelAssemblyNames" and decorated by DataContractAttribute will be exposed to client programs.

By default, the cherry-picking of Web API functions is opt-out, so all public functions of ApiController derived classes will be processed, except those declared in "ExcludedControllerNames".

{
    "ClientLibraryProjectFolderName": "DemoWebApi.ClientApi",
    "ExcludedControllerNames": [
        "DemoWebApi.Controllers.Account"
    ],
    "GenerateBothAsyncAndSync": true,
    "TypeScriptFolder": "ClientApi",
    "DataModelAssemblyNames": [
        "DemoWebApi.DemoData", "DemoWebApi"
    ],
    "CherryPickingMethods": 1
}

WebApiClientGen encourages opt-in for cherry-picking data types. However, if you have large amount of data types to be exposed to the client side and you prefer opt-out, you may choose "NewtonsoftJson", "Serializable" (4), "AspNet" (8) or "All" (0) of CherryPickingMethods. Please note, overall only custom data types implemented in assemblies declared by "DataModelAssemblyNames" are included, and this is opt-in.

When "Serializable" is selected, data members decorated by NonSerializedAttribute will be excluded.

When "AspNet" or "All" is selected, all data members will be exposed to the client sides.

Please refer to examples of cherry picking.

Remarks:

ASP.NET MVC / Web API Help Page has such logic as illustrated in the code snippet below.

        private static bool ShouldDisplayMember(MemberInfo member, bool hasDataContractAttribute)
        {
            JsonIgnoreAttribute jsonIgnore = member.GetCustomAttribute<JsonIgnoreAttribute>();
            XmlIgnoreAttribute xmlIgnore = member.GetCustomAttribute<XmlIgnoreAttribute>();
            IgnoreDataMemberAttribute ignoreDataMember = member.GetCustomAttribute<IgnoreDataMemberAttribute>();
            NonSerializedAttribute nonSerialized = member.GetCustomAttribute<NonSerializedAttribute>();
            ApiExplorerSettingsAttribute apiExplorerSetting = member.GetCustomAttribute<ApiExplorerSettingsAttribute>();

            bool hasMemberAttribute = member.DeclaringType.IsEnum ?
                member.GetCustomAttribute<EnumMemberAttribute>() != null :
                member.GetCustomAttribute<DataMemberAttribute>() != null;

            // Display member only if all the followings are true:
            // no JsonIgnoreAttribute
            // no XmlIgnoreAttribute
            // no IgnoreDataMemberAttribute
            // no NonSerializedAttribute
            // no ApiExplorerSettingsAttribute with IgnoreApi set to true
            // no DataContractAttribute without DataMemberAttribute or EnumMemberAttribute
            return jsonIgnore == null &&
                xmlIgnore == null &&
                ignoreDataMember == null &&
                nonSerialized == null &&
                (apiExplorerSetting == null || !apiExplorerSetting.IgnoreApi) &&
                (!hasDataContractAttribute || hasMemberAttribute);
        }

WebApiClientGen does not deal with XmlIgnoreAttribute since NoeSerializedAttribute is more appropriate in Web API designs.

Clone this wiki locally