From f8b40a0a26596bfc5df6e8ee9d248aeb6c8ac8d1 Mon Sep 17 00:00:00 2001 From: Yuhan Hao Date: Mon, 13 Nov 2023 13:12:12 -0800 Subject: [PATCH] (bugfix) fix getMessage generation for @thrift.ErrorMessage in Java Summary: D50940922 introduced `thrift.ErrorMessage`, but it contains a small bug where java error message may be swallowed. in this diff, we fixed this bug. Reviewed By: prakashgayasen, thedavekwon Differential Revision: D51224128 fbshipit-source-id: 84d1f4553d6f2f645c7be54c16daf5a0ee32e261 --- thrift/compiler/generate/t_mstch_java_generator.cc | 11 +++++++---- .../exceptions/ExceptionWithStructuredAnnotation.java | 6 +++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/thrift/compiler/generate/t_mstch_java_generator.cc b/thrift/compiler/generate/t_mstch_java_generator.cc index cfdc2f04e66..4a7b2ee33be 100644 --- a/thrift/compiler/generate/t_mstch_java_generator.cc +++ b/thrift/compiler/generate/t_mstch_java_generator.cc @@ -588,11 +588,14 @@ class mstch_java_struct : public mstch_struct { return get_java_swift_name(message_field); } // we can only override Throwable's getMessage() if: - // 1 - there is provided 'message' annotation - // 2 - there is no struct field named 'message' - // (since it will generate getMessage() as well) + // 1 - there is either provided `'message'` annotation or + // `@thrift.ErrorMessage` annotation. + // 2 - there is no struct field named 'message' (since it + // will generate `getMessage()` method) mstch::node needs_exception_message() { - return struct_->is_exception() && struct_->has_annotation("message") && + return struct_->is_exception() && + dynamic_cast(*struct_).get_message_field() != + nullptr && struct_->get_field_by_name("message") == nullptr; } }; diff --git a/thrift/compiler/test/fixtures/exceptions/gen-java/test/fixtures/exceptions/ExceptionWithStructuredAnnotation.java b/thrift/compiler/test/fixtures/exceptions/gen-java/test/fixtures/exceptions/ExceptionWithStructuredAnnotation.java index 9b7c730f799..732aeeb7e56 100644 --- a/thrift/compiler/test/fixtures/exceptions/gen-java/test/fixtures/exceptions/ExceptionWithStructuredAnnotation.java +++ b/thrift/compiler/test/fixtures/exceptions/gen-java/test/fixtures/exceptions/ExceptionWithStructuredAnnotation.java @@ -103,7 +103,11 @@ public ExceptionWithStructuredAnnotation build() { @com.facebook.swift.codec.ThriftField(value=2, name="error_code", requiredness=Requiredness.NONE) public int getErrorCode() { return errorCode; } - + + @java.lang.Override + public String getMessage() { + return messageField; + } public static com.facebook.thrift.payload.Reader asReader() { return ExceptionWithStructuredAnnotation::read0;