Git Product home page Git Product logo

Comments (3)

cowtowncoder avatar cowtowncoder commented on September 26, 2024

Please include actual code you are using: textual description is not enough to know what is being attempted.

Note, though, that the exception comes from Apache Avro library which does validation for generated schema.

from jackson-dataformats-binary.

pfr-enedis avatar pfr-enedis commented on September 26, 2024

Hi, @cowtowncoder , here is the reproductible example,
it seems the reproduction behaviour, is more linked to the JsonValue annotation,
when commenting this annotation, this work, and fail if the annotation is present

import java.util.HashMap;
import java.util.Map;

import javax.annotation.processing.Generated;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.dataformat.avro.AvroMapper;
import com.fasterxml.jackson.dataformat.avro.jsr310.AvroJavaTimeModule;
import com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaGenerator;


/**
 * class to reproduce an avro error, for jackson databind
 */
public class GenerateAvroError {

    @Generated("jsonschema2pojo")
    public enum EnumTypeContrat {

        CARD_S("CARD-S"),
        CARD_ELD("CARD-ELD"),
        GRD_F("GRD-F"),
        CSD("CSD"),
        GRD_RE("GRD-RE"),
        PROTOC_501("PROTOC-501"),
        CRAE("CRAE"),
        CARD_I("CARD-I"),
        CSD_I("CSD-I");
        private final String value;
        private final static Map<String, EnumTypeContrat> CONSTANTS = new HashMap<String,EnumTypeContrat>();

        static {
            for (EnumTypeContrat c: values()) {
                CONSTANTS.put(c.value, c);
            }
        }

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

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

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

        @JsonCreator
        public static EnumTypeContrat fromValue(String value) {
            EnumTypeContrat constant = CONSTANTS.get(value);
            if (constant == null) {
                throw new IllegalArgumentException(value);
            } else {
                return constant;
            }
        }

    }

	public static class DummyClassToReproduceError {

		private EnumTypeContrat contract;

		@JsonProperty("contr")
		public EnumTypeContrat getContract() {
			return contract;
		}

	}
	

	@Test
	public void testSerializeAvro() throws Exception {

		AvroSchemaGenerator gen = new AvroSchemaGenerator();

		AvroMapper avroMapper = AvroMapper.builder().disable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)
				.addModule(new AvroJavaTimeModule()).build();

		gen.enableLogicalTypes();
		avroMapper.acceptJsonFormatVisitor(DummyClassToReproduceError.class, gen);
		com.fasterxml.jackson.dataformat.avro.AvroSchema schemaWrapper = gen.getGeneratedSchema();

		org.apache.avro.Schema avroSchema = schemaWrapper.getAvroSchema();
		String avroSchemaInJSON = avroSchema.toString(true);

		System.out.println(avroSchemaInJSON);
		
	}
	
	
}

the associated backtrace :

org.apache.avro.SchemaParseException: Illegal character in: CARD-S
	at org.apache.avro.Schema.validateName(Schema.java:1566)
	at org.apache.avro.Schema.access$400(Schema.java:91)
	at org.apache.avro.Schema$EnumSchema.<init>(Schema.java:1035)
	at org.apache.avro.Schema.createEnum(Schema.java:227)
	at com.fasterxml.jackson.dataformat.avro.schema.AvroSchemaHelper.createEnumSchema(AvroSchemaHelper.java:272)
	at com.fasterxml.jackson.dataformat.avro.schema.StringVisitor.builtAvroSchema(StringVisitor.java:54)
	at com.fasterxml.jackson.dataformat.avro.schema.VisitorFormatWrapperImpl.getAvroSchema(VisitorFormatWrapperImpl.java:103)
	at com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor.schemaFieldForWriter(RecordVisitor.java:175)
	at com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor.optionalProperty(RecordVisitor.java:117)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.depositSchemaProperty(BeanPropertyWriter.java:843)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.acceptJsonFormatVisitor(BeanSerializerBase.java:914)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.acceptJsonFormatVisitor(DefaultSerializerProvider.java:565)
	at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:4670)
	at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:4649)
	at xxxxxx.testSerializeAvro(GenerateAvroError.java:92)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)


from jackson-dataformats-binary.

cowtowncoder avatar cowtowncoder commented on September 26, 2024

Thank you @pfr-enedis for the reproduction.

Yes, you are probably right wrt @JsonValue.

from jackson-dataformats-binary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.