Posts in category “Programming”

Fix swagger stuck on extend an API: How to configure Swashbuckle to ignore property on model

Referenced

I found the root cause is that the JSON generated by swagger is too large: Swagger outputs over 18000 lines for that API! Further research told me that two property fields contribute about 17000 lines. So how to configure swagger to ignore these two properties is the key. After googling a while, I found VeganHunter's answer in this thread Reference is the simplest.

Solution for .NET Core 3.1 and .NET Standard 2.1:

Use JsonIgnore from System.Text.Json.Serialization namespace.

( JsonIgnore from Newtonsoft.Json will NOT work )

public class Test
{
    [System.Text.Json.Serialization.JsonIgnore]
    public int HiddenProperty { get; set; }
    public int VisibleProperty { get; set; }
}

Hope it could also help someone else. 😀

A tricky issue about [getImageInfo:fail image not found] painter component for WeChat Miniprogram

I heard my colleague Joe met a tricky bug this morning and he has no idea about it. After a brief google, I found it's a rather famous one. So many people met this issue and most of them haven't got an answer.

So I dug it deeper and found a clue that locates at https://developers.weixin.qq.com/community/develop/doc/0000c6d4b6c2d0cb128a1c0475b000?_at=1566630830developers.weixin.qq.com

and eventually, I found my solution. I shared it at https://developers.weixin.qq.com/community/develop/doc/000ac29db444e0b922c6caee759c00

PS: there's a common phenomenon among developers in China. Someone asks a question, and many people replyed they have met the same issue. But, no answers. I don't think all of them haven't found out their answer. People are interested in asking questions but have less interest to answer, even they finally got a valuable answer. It's a shame. I won't be that kind of person.

Don't simple unset them afterward when using `shopt` change a setting

Sometimes we need to run shopt -s dotglob nullglob before moving files including dotfiles. So there's another question, do we need to set it back afterward? The most correct answer is

It's usually not clear if either dotglob or nullglob were already set before running shopt -s to set them. Thus, blindly un-setting them may not be the proper reset to do. Setting them in a subshell would leave the current shell's settings unchanged:

( shopt -s dotglob nullglob; mv /public/* /public_html/ )

Reference: Jeff Schaller's answer under this question

Tuple type in C#

It's a rather interesting feature. I first use it the same way as the python tuple type. I immediately found I was wrong. It doesn't support using an index to visit certain element

Stupid enough. I think. Soon I found the correct way, you know, the Item1, Item2 way.

It's so Stupid! Then I found the best way: the named element way.

Task<(List<string> orderIdList, List<string> orderNoList)> GetExpiringOrderIdListAndOrderNoList(DateTime checkTime);

Ok. It's not very stupid.

.NET Core: Set the correct CurrentCulture basing the `lang` header

  1. Create a middleware to do that task

     public static void SetCurrentCulture(this IApplicationBuilder app)
     {
         app.Use(async (context, next) =>
         {
             var cultureQuery = context.Request.Headers["lang"] == "en" ? "en-US" : "zh-CN";
             if (!string.IsNullOrWhiteSpace(cultureQuery))
             {
                 var culture = new CultureInfo(cultureQuery);
                 CultureInfo.CurrentCulture = culture;
                 CultureInfo.CurrentUICulture = culture;
             }
             await next();
         });
     }
    
  2. Use the middleware in Startup.cs

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILog logger,
         IExceptionEntityService exceptionEntityService)
     {
         ...
         app.SetCurrentCulture();
         ...
     }