Skip to content

Commit

Permalink
AVRO-2918: rebased done
Browse files Browse the repository at this point in the history
  • Loading branch information
clesaec committed Sep 12, 2023
1 parent 1085d30 commit 050fb06
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lang/java/avro/src/main/java/org/apache/avro/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,19 @@ public List<Field> getFields() {
}

/**
* If this is a record, returns whether the fields have been set.
* If this is a record, return if it or parent have fields.
*/
public boolean hasFields() {
throw new AvroRuntimeException("Not a record: " + this);
}

/**
* If this is a record, returns whether the fields have been set.
*/
protected boolean hasDeclaredFields() {
throw new AvroRuntimeException("Not a record: " + this);
}

public boolean hasChild() {
throw new AvroRuntimeException("Not a record: " + this);
}
Expand Down Expand Up @@ -1082,6 +1089,11 @@ public boolean hasFields() {
return fields != null;
}

@Override
protected boolean hasDeclaredFields() {
return fields != null;
}

@Override
public RecordSchema findInHierachy(int index) {
if (this.index == index) {
Expand Down Expand Up @@ -2204,8 +2216,9 @@ static Schema parseCompleteSchema(JsonNode schema, Names names, String currentSp
final boolean isTypeError = "error".equals(type);
final boolean isTypeRecord = "record".equals(type);
final boolean isTypeArray = "array".equals(type);
final boolean isTypeExtendedRecord = type != null && (type.startsWith("record:") || type.startsWith("error:"));

if (isTypeRecord || isTypeError || "enum".equals(type) || "fixed".equals(type)) {
if (isTypeExtendedRecord || isTypeRecord || isTypeError || "enum".equals(type) || "fixed".equals(type)) {
// named schema
String space = getOptionalText(schema, "namespace");

Expand All @@ -2218,8 +2231,8 @@ static Schema parseCompleteSchema(JsonNode schema, Names names, String currentSp
throw new SchemaParseException("Unparsed field type " + name);
}
}
if (isTypeRecord || isTypeError) {
if (result != null && !result.hasFields()) {
if (isTypeRecord || isTypeError || isTypeExtendedRecord) {
if (result != null && !result.hasDeclaredFields()) {
final List<Field> fields = new ArrayList<>();
JsonNode fieldsNode = schema.get("fields");
if (fieldsNode == null || !fieldsNode.isArray())
Expand Down Expand Up @@ -2285,7 +2298,7 @@ static Schema resolveSchema(JsonNode schema, Names names, String currentNameSpac
fullName.append(nodeName);
Schema schema1 = names.get(fullName.toString());

if (schema1 != null && schema1.getType() == Type.RECORD && !schema1.hasFields()) {
if (schema1 != null && schema1.getType() == Type.RECORD && !schema1.hasDeclaredFields()) {
Schema.parseCompleteSchema(schema, names, np);
}
return schema1;
Expand Down
8 changes: 8 additions & 0 deletions lang/java/avro/src/test/java/org/apache/avro/TestSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,24 @@
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type;
import org.apache.avro.generic.GenericData;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TestSchema {
Expand Down

0 comments on commit 050fb06

Please sign in to comment.