JSON serialization - extensions of types OffsetDateTime and bytes[] deserialized as Strings

Đang mở
#699 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
42/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu với bản tái hiện JsonSerializationTest và các điểm vào CloudEventJsonSerializer và CloudEventJsonDeserializer được hiển thị trong issue. Chạy bài kiểm thử với các phần mở rộng OffsetDateTime và bytes[] được bật, sau đó theo dõi quá trình xử lý định dạng JSON của EventFormatProvider. Công việc được xem là hoàn tất khi các giá trị phần mở rộng đó giữ nguyên kiểu ban đầu và các assertions đạt sau khi tuần tự hóa round-trip.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Extensions of types OffsetDateTime and bytes[] are not restored properly after serialization and deserialization using JSON format. Values are Strings.

class JsonSerializationTest {

  final CloudEventJsonSerializer serializer = new CloudEventJsonSerializer();
  final CloudEventJsonDeserializer deserializer = new CloudEventJsonDeserializer();

  @Test
  void shouldSerializeAndDeserialize() {
    CloudEvent serialized = createTestEvent();

    Buffer buffer = serializer.serialize(serialized);
    CloudEvent deserialized = deserializer.deserialize(buffer);

    assertEvent(serialized, deserialized);
  }

  private CloudEvent createTestEvent() {
    return CloudEventBuilder.v1()
        .withId("id")
        .withSource(URI.create("source"))
        .withType("type")
        .withDataContentType("datacontenttype")
        .withDataSchema(URI.create("https://data/schema"))
        .withSubject("subject")
        .withTime(OffsetDateTime.now(ZoneId.of("UTC")))
        .withExtension("extstring", "extstring-value")
        .withExtension("extboolean", true)
        .withExtension("extint", 123)
        // Not working, deserialized value is string
        // .withExtension("exttime", OffsetDateTime.now(ZoneId.of("UTC")))
        // Not working, deserialized value is string
        // .withExtension("extbytes", "bytes".getBytes(StandardCharsets.UTF_8))
        .withData("test data".getBytes(StandardCharsets.UTF_8))
        .build();
  }

  private void assertEvent(CloudEvent expected, CloudEvent actual) {
    assertNotNull(actual);
    assertEquals(expected.getId(), actual.getId());
    assertEquals(expected.getSource(), actual.getSource());
    assertEquals(expected.getSpecVersion(), actual.getSpecVersion());
    assertEquals(expected.getType(), actual.getType());
    assertEquals(expected.getDataContentType(), actual.getDataContentType());
    assertEquals(expected.getDataSchema(), actual.getDataSchema());
    assertEquals(expected.getSubject(), actual.getSubject());
    assertEquals(expected.getTime(), actual.getTime());
    assertEquals(expected.getData(), actual.getData());
    assertEquals(expected.getExtensionNames(), actual.getExtensionNames());
    expected.getExtensionNames().forEach(extName -> {
          if (expected.getExtension(extName) instanceof byte[]) {
            assertInstanceOf(byte[].class, actual.getExtension(extName),
                "Extension " + extName + " is not a byte[]");
            assertArrayEquals((byte[]) expected.getExtension(extName),
                (byte[]) actual.getExtension(extName),
                "Values not equal for extension " + extName);
          } else {
            assertEquals(expected.getExtension(extName), actual.getExtension(extName),
                "Values not equal for extension " + extName);
          }
        }
    );
  }
}
public class CloudEventJsonSerializer implements Serializer<CloudEvent> {

  @Override
  public boolean handles(Object payload) {
    return payload instanceof CloudEvent;
  }

  @Override
  public Buffer serialize(CloudEvent payload) {
    byte[] serialized = Objects.requireNonNull(
            EventFormatProvider
                .getInstance()
                .resolveFormat(ContentType.JSON),
            "JSON event format dependency missing")
        .serialize(payload);
    return Buffer.buffer(serialized);
  }
}
public class CloudEventJsonDeserializer implements Deserializer<CloudEvent> {

  @Override
  public CloudEvent deserialize(Buffer payload) {
    return Objects.requireNonNull(
            EventFormatProvider
                .getInstance()
                .resolveFormat(ContentType.JSON),
            "JSON event format dependency missing")
        .deserialize(payload.getBytes());
  }
}
Ngôn ngữ chính
Java
Star
446
Fork
172
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloudevents/sdk-java

Tất cả issue của cloudevents/sdk-java

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.