You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "clesaec (via GitHub)" <gi...@apache.org> on 2023/07/18 13:55:03 UTC

[GitHub] [avro] clesaec opened a new pull request, #2364: AVRO-2236: [java] default value for record type

clesaec opened a new pull request, #2364:
URL: https://github.com/apache/avro/pull/2364

   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/master/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, commits follow the guidelines from [How to write a good git commit message](https://chris.beams.io/posts/git-commit/)
       1. Subject is separated from body by a blank line
       1. Subject is limited to 50 characters (not including Jira issue reference)
       1. Subject does not end with a period
       1. Subject uses the imperative mood ("add", not "adding")
       1. Body wraps at 72 characters
       1. Body explains "what" and "why", not "how"
   
   -->
   
   ## What is the purpose of the change
   
   [AVRO-2236](https://issues.apache.org/jira/browse/AVRO-2236) : Add default value for record type.
   This PR allow a field is of type record to have a default value even if the record itself have field with other default values (with union type).
   
   ## Verifying this change
   
   - new unit test TestSchemaValidateDefault
   
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (no)
   - If yes, how is the feature documented? (not applicable)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [avro] dkulp merged pull request #2364: AVRO-2236: [java] default value for record type

Posted by "dkulp (via GitHub)" <gi...@apache.org>.
dkulp merged PR #2364:
URL: https://github.com/apache/avro/pull/2364


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [avro] github-code-scanning[bot] commented on a diff in pull request #2364: AVRO-2236: [java] default value for record type

Posted by "github-code-scanning[bot] (via GitHub)" <gi...@apache.org>.
github-code-scanning[bot] commented on code in PR #2364:
URL: https://github.com/apache/avro/pull/2364#discussion_r1267252706


##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaValidateDefault.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.reflect.ReflectData;
+import org.apache.avro.reflect.ReflectDatumReader;
+import org.apache.avro.reflect.ReflectDatumWriter;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Function;
+
+public class TestSchemaValidateDefault {
+
+  @Test
+  public void valueReadWithCorrectDefaultValue() throws IOException {
+
+    Schema schm = ReflectData.get().getSchema(ExampleRecord.class);

Review Comment:
   ## Unread local variable
   
   Variable 'Schema schm' is never read.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/3034)



##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaValidateDefault.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.reflect.ReflectData;
+import org.apache.avro.reflect.ReflectDatumReader;
+import org.apache.avro.reflect.ReflectDatumWriter;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Function;
+
+public class TestSchemaValidateDefault {
+
+  @Test
+  public void valueReadWithCorrectDefaultValue() throws IOException {
+
+    Schema schm = ReflectData.get().getSchema(ExampleRecord.class);
+    ExampleRecord writtenValue = new ExampleRecord(new ComplexValue(42L), new ComplexValue(666L));
+    byte[] bytes = getSerializer(ExampleRecord.SCHEMA_WITH_ONE_FIELD).apply(writtenValue);
+
+    // Function<byte[], ExampleRecord> deserializer =
+    // getDeserializer(ExampleRecord.class, ExampleRecord.SCHEMA_WITH_TWO_FIELDS,
+    // ExampleRecord.SCHEMA_WITH_ONE_FIELD);
+    // ExampleRecord deserializedValue = deserializer.apply(bytes);
+
+    ReflectDatumReader<ExampleRecord> reader = new ReflectDatumReader<>(ExampleRecord.SCHEMA_WITH_ONE_FIELD,
+        ExampleRecord.SCHEMA_WITH_TWO_FIELDS, ReflectData.get());
+    Decoder decoder = DecoderFactory.get().jsonDecoder(ExampleRecord.SCHEMA_WITH_ONE_FIELD,
+        new ByteArrayInputStream(bytes));
+    ExampleRecord deserializedValue = reader.read(null, decoder);
+
+    Assertions.assertNotNull(deserializedValue.getValue2(), "Null get value2");
+    Assertions.assertEquals(15L, deserializedValue.getValue2().getValue());
+  }
+
+  public static Function<Object, byte[]> getSerializer(Schema writerSchema) {
+    Objects.requireNonNull(writerSchema, "writerSchema must not be null");
+
+    ReflectDatumWriter<Object> writer = new ReflectDatumWriter<>(writerSchema, new ReflectData());
+    return object -> {
+      try {
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        Encoder encoder = EncoderFactory.get().jsonEncoder(writerSchema, stream);
+        writer.write(object, encoder);
+        encoder.flush();
+        return stream.toByteArray();
+      } catch (IOException e) {
+        throw new IllegalStateException(String.format("Avro failed to encode %s to schema %s", object, writerSchema),
+            e);
+      }
+    };
+  }
+
+  public static <T> Function<byte[], T> getDeserializer(Class<T> readClass, Schema readerSchema, Schema writerSchema) {
+    Objects.requireNonNull(readClass, "readClass must not be null");
+    Objects.requireNonNull(readerSchema, "readerSchema must not be null");
+    Objects.requireNonNull(writerSchema, "writerSchema must not be null");
+
+    ReflectDatumReader<T> reader = new ReflectDatumReader<>(writerSchema, readerSchema, new ReflectData());
+    return bytes -> {
+      try {
+        Decoder decoder = DecoderFactory.get().jsonDecoder(writerSchema, new ByteArrayInputStream(bytes));
+        T readValue = reader.read(null, decoder);
+        return readValue;
+      } catch (IOException e) {
+        throw new IllegalStateException(String.format("Avro failed to decode %s to %s", bytes, readClass), e);
+      }
+    };
+  }
+
+  static final Schema SCHEMA = SchemaBuilder.record("org.apache.avro.TestSchemaValidateDefault.ComplexValue").fields()
+      .optionalLong("value").endRecord();
+
+  public static class ComplexValue {
+
+    private Long value;
+
+    public ComplexValue() {
+    }
+
+    public ComplexValue(Long value) {
+      this.value = value;
+    }
+
+    public Long getValue() {
+      return this.value;
+    }
+
+    @Override
+    public String toString() {
+      return "{" + "\"value\": { \"long\": " + this.value + "}}";
+    }
+  }
+
+  public static class ExampleRecord {
+    public static final Schema SCHEMA_WITH_ONE_FIELD;
+    public static final Schema SCHEMA_WITH_TWO_FIELDS;
+
+    static {
+      Schema schm = ReflectData.get().getSchema(ExampleRecord.class);

Review Comment:
   ## Unread local variable
   
   Variable 'Schema schm' is never read.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/3035)



##########
lang/java/avro/src/test/java/org/apache/avro/TestSchemaValidateDefault.java:
##########
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import org.apache.avro.generic.GenericData;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.avro.reflect.ReflectData;
+import org.apache.avro.reflect.ReflectDatumReader;
+import org.apache.avro.reflect.ReflectDatumWriter;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Function;
+
+public class TestSchemaValidateDefault {
+
+  @Test
+  public void valueReadWithCorrectDefaultValue() throws IOException {
+
+    Schema schm = ReflectData.get().getSchema(ExampleRecord.class);
+    ExampleRecord writtenValue = new ExampleRecord(new ComplexValue(42L), new ComplexValue(666L));
+    byte[] bytes = getSerializer(ExampleRecord.SCHEMA_WITH_ONE_FIELD).apply(writtenValue);
+
+    // Function<byte[], ExampleRecord> deserializer =
+    // getDeserializer(ExampleRecord.class, ExampleRecord.SCHEMA_WITH_TWO_FIELDS,
+    // ExampleRecord.SCHEMA_WITH_ONE_FIELD);
+    // ExampleRecord deserializedValue = deserializer.apply(bytes);
+
+    ReflectDatumReader<ExampleRecord> reader = new ReflectDatumReader<>(ExampleRecord.SCHEMA_WITH_ONE_FIELD,
+        ExampleRecord.SCHEMA_WITH_TWO_FIELDS, ReflectData.get());
+    Decoder decoder = DecoderFactory.get().jsonDecoder(ExampleRecord.SCHEMA_WITH_ONE_FIELD,
+        new ByteArrayInputStream(bytes));
+    ExampleRecord deserializedValue = reader.read(null, decoder);
+
+    Assertions.assertNotNull(deserializedValue.getValue2(), "Null get value2");
+    Assertions.assertEquals(15L, deserializedValue.getValue2().getValue());
+  }
+
+  public static Function<Object, byte[]> getSerializer(Schema writerSchema) {
+    Objects.requireNonNull(writerSchema, "writerSchema must not be null");
+
+    ReflectDatumWriter<Object> writer = new ReflectDatumWriter<>(writerSchema, new ReflectData());
+    return object -> {
+      try {
+        ByteArrayOutputStream stream = new ByteArrayOutputStream();
+        Encoder encoder = EncoderFactory.get().jsonEncoder(writerSchema, stream);
+        writer.write(object, encoder);
+        encoder.flush();
+        return stream.toByteArray();
+      } catch (IOException e) {
+        throw new IllegalStateException(String.format("Avro failed to encode %s to schema %s", object, writerSchema),
+            e);
+      }
+    };
+  }
+
+  public static <T> Function<byte[], T> getDeserializer(Class<T> readClass, Schema readerSchema, Schema writerSchema) {
+    Objects.requireNonNull(readClass, "readClass must not be null");
+    Objects.requireNonNull(readerSchema, "readerSchema must not be null");
+    Objects.requireNonNull(writerSchema, "writerSchema must not be null");
+
+    ReflectDatumReader<T> reader = new ReflectDatumReader<>(writerSchema, readerSchema, new ReflectData());
+    return bytes -> {
+      try {
+        Decoder decoder = DecoderFactory.get().jsonDecoder(writerSchema, new ByteArrayInputStream(bytes));
+        T readValue = reader.read(null, decoder);
+        return readValue;
+      } catch (IOException e) {
+        throw new IllegalStateException(String.format("Avro failed to decode %s to %s", bytes, readClass), e);

Review Comment:
   ## Implicit conversion from array to string
   
   Implicit conversion from Array to String.
   
   [Show more details](https://github.com/apache/avro/security/code-scanning/3033)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org