Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 6, 2025
2 parents babb8da + da74291 commit b1c0bda
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
import org.apache.avro.JsonProperties;
import org.apache.avro.Schema;
import org.apache.avro.Schema.Parser;
import org.apache.avro.SchemaParseException;
import org.apache.avro.reflect.AvroAlias;
import org.apache.avro.reflect.Stringable;
import org.apache.avro.specific.SpecificData;

import tools.jackson.core.JacksonException;
import tools.jackson.core.JsonGenerator;
import tools.jackson.core.JsonParser;

import tools.jackson.databind.*;
import tools.jackson.databind.cfg.MapperConfig;
import tools.jackson.databind.exc.InvalidDefinitionException;
import tools.jackson.databind.introspect.AnnotatedClass;
import tools.jackson.databind.introspect.AnnotatedConstructor;
import tools.jackson.databind.json.JsonMapper;
Expand Down Expand Up @@ -257,19 +260,26 @@ public static Schema parseJsonSchema(String json) {
*/
public static Schema createEnumSchema(MapperConfig<?> config, JavaType enumType,
AnnotatedClass annotations, List<String> values) {
return addAlias(Schema.createEnum(
getTypeName(enumType),
config.getAnnotationIntrospector().findClassDescription(config, annotations),
getNamespace(enumType, annotations), values
), annotations);
final Schema avroSchema;
try {
avroSchema = Schema.createEnum(
getTypeName(enumType),
config.getAnnotationIntrospector().findClassDescription(config, annotations),
getNamespace(enumType, annotations),
values);
} catch (SchemaParseException spe) {
final String msg = String.format("Problem generating Avro `Schema` for Enum type %s: %s",
ClassUtil.getTypeDescription(enumType), spe.getMessage());
throw InvalidDefinitionException.from((JsonGenerator) null, msg, enumType);
}

return addAlias(avroSchema, annotations);
}

/**
* Helper method to enclose details of expressing best Avro Schema for
* {@link java.util.UUID}: 16-byte fixed-length binary (alternative would
* be basic variable length "bytes").
*
* @since 2.11
*/
public static Schema createUUIDSchema() {
return Schema.createFixed("UUID", null, "java.util", 16);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package tools.jackson.dataformat.avro.schema;

import java.util.ArrayList;
import java.util.Set;

import tools.jackson.databind.*;
import tools.jackson.databind.introspect.AnnotatedClass;
import tools.jackson.databind.jsonFormatVisitors.JsonStringFormatVisitor;

import org.apache.avro.Schema;

import java.util.ArrayList;
import java.util.Set;

/**
* Specific visitor for Java Enum types that are to be exposed as
* Avro Enums. Used unless Java Enums are to be mapped to Avro Strings.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tools.jackson.dataformat.avro.schema;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonValue;

import tools.jackson.databind.exc.InvalidDefinitionException;
import tools.jackson.dataformat.avro.AvroMapper;
import tools.jackson.dataformat.avro.AvroTestBase;

// For [dataformats-binary#422]
public class EnumSchema422Test extends AvroTestBase
{
enum EnumType422 {
CARD_S("CARD-S");

private final String value;

EnumType422(String value) {
this.value = value;
}

@JsonValue
public String value() {
return this.value;
}
}

static class Wrapper422 {
public EnumType422 contract;
}

private final AvroMapper MAPPER = newMapper();

// For [dataformats-binary#422]
@Test
public void testEnumSchemaGeneration422() throws Exception
{
// First, failure due to invalid enum value (when generating as Enum)
AvroSchemaGenerator gen = new AvroSchemaGenerator()
.enableLogicalTypes();
try {
MAPPER.acceptJsonFormatVisitor(Wrapper422.class, gen);
fail("Expected failure");
} catch (InvalidDefinitionException e) { // in 2.x
verifyException(e, "Problem generating Avro `Schema` for Enum type");
verifyException(e, "Illegal character in");
}

// But then success when configuring to produce Strings for Enum types

gen = new AvroSchemaGenerator()
.enableLogicalTypes()
.enableWriteEnumAsString();
MAPPER.acceptJsonFormatVisitor(Wrapper422.class, gen);

org.apache.avro.Schema avroSchema = gen.getGeneratedSchema().getAvroSchema();
String avroSchemaInJSON = avroSchema.toString(true);
assertNotNull(avroSchemaInJSON);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import static org.assertj.core.api.Assertions.assertThat;

public class Enum_schemaCreationTest extends AvroTestBase {

public class Enum_schemaCreationTest extends AvroTestBase
{
static enum NumbersEnum {
ONE, TWO, THREE
}
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Active maintainers:
#308: (avro) Incorrect serialization for `LogicalType.Decimal` (Java `BigDecimal`)
(reported by Idan S)
(fix contributed by Michal F)
#422: Avro generation failed with enums containing values with special characters
(reported by @pfr-enedis)
#535: (avro) AvroSchemaGenerator: logicalType(s) never set for non-date classes
(reported by Cormac R)
(fix contributed by Michal F)
Expand Down

0 comments on commit b1c0bda

Please sign in to comment.