Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 当 属性为 public 修饰的 枚举类型时,其序列化结果与 getter 方法的返回值不一致 #1727

Closed
CodePlayer opened this issue Aug 9, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@CodePlayer
Copy link
Contributor

问题描述

当 Java Bean 中存在一个 public 关键字修饰的 枚举类型属性时,Fastjson 2 将忽略 对应 getter 方法的输出,并强制使用该枚举属性直接输出。
只有去掉 public 关键字,才能优先使用 getter 的返回值作为属性输出。

环境信息

  • OS信息: Windows 10
  • JDK信息:JDK 17.0.6
  • 版本信息:Fastjson2 2.0.38

重现步骤

参考如下代码:

	public static enum Color {

		RED(1),
		YELLOW(3),
		BLUE(5),
		;

		final Integer value;

		Color(Integer value) {
			this.value = value;
		}
	}

	@Getter
	public static class Bean {

		public Color color;

		public Integer getColor() {
			return color.value;
		}

	}

	@Test
	public void test() {
		final Bean bean = new Bean();
		bean.color = Color.BLUE;
		
		// 当 Bean.color 有 public 关键字修饰时,将输出 {"color":"BLUE"}
               // 去掉 Bean.color 的 public 关键字后,将输出 {"color":5}
		System.out.println(JSON.toJSONString(bean));
	}

期待的正确结果

除非指定了 JSONReader.Feature.FieldBased ,否则都应该默认优先基于 getter 的返回值进行序列化输出。

相关日志输出

附加信息

@CodePlayer CodePlayer added the bug Something isn't working label Aug 9, 2023
@wenshao wenshao added this to the 2.0.39 milestone Aug 13, 2023
@wenshao
Copy link
Member

wenshao commented Aug 13, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.39-SNAPSHOT/
问题已修复,请帮忙用2.0.39-SNAPSHOT版本验证,2.0.39版本预计在8月27日前发布

@wenshao
Copy link
Member

wenshao commented Aug 15, 2023

@wenshao wenshao closed this as completed Aug 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants