fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] 2.0.57 Deserialize an optional null parameter deserealize as empty string

Open JordiMartinezVicent opened this issue 6 months ago • 2 comments

This is a bug of the fastjson2 version 2.0.57. With the previous version (2.0.56) it works as expected.

Given the following json:

{  
  "bundleProductSummaries": [{      
      "detail": {        
        "colors": [
          {            
            "certifiedMaterials": {
              "show": false,
              "materials": []
            }
          },
          {            
            "certifiedMaterials": {
              "show": false,
              "materials": []
            }
          },
          {            
            "certifiedMaterials": {
              "show": false,
              "materials": []
            }
          }
        ]
      }
    }]
}

Consider the following test:

@Test
  void productBrandsProduct() {

    var is = this.getClass().getResourceAsStream("/ProductBrandsProduct.json");

    ProductBrandsProduct dto = JSON.parseObject(is, ProductBrandsProduct.class);

    assertThat(dto).isNotNull();
    assertThat(dto.getBundleProductSummaries().get(0).getDetail().getColors().get(0).getCertifiedMaterials().getContainsAtLeastTitle())
        .isNull();
  }

Note that 'containsAtLeastTitle' is not included at the json as it is an optional attribute.

The test fails because getContainsAtLeastTitle="" (empty string).

The test works well with the previous version 2.0.56

I have attached the following project with the given test:

fastjson2.zip

JordiMartinezVicent avatar Jun 03 '25 07:06 JordiMartinezVicent

Thank you for your feedback. I'll go research it.

jujn avatar Jun 04 '25 05:06 jujn

Hi @JordiMartinezVicent, It looks fixed on 2.59.0, even when this issue is still open now - I just tested it now. Let's talk internally on Teams on monday. Regards,

spanishkangaroo avatar Nov 14 '25 14:11 spanishkangaroo