reinert / JJSchema

A generator from Java Types to JSON-Schema using Jackson.
Other
117 stars 56 forks source link

StringIndexOutOfBoundsException on SomeObject#get() #85

Closed ehues closed 6 years ago

ehues commented 6 years ago

Methods named get() cause a StringIndexOutOfBoundsException. I noticed this when I was trying to build a schema containing an Optional. e.g.

import com.fasterxml.jackson.databind.JsonNode;
import com.github.reinert.jjschema.v1.JsonSchemaFactory;
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory;

public class SchemaGenerator {

    public static class BadClass {
        public double get() {
            return 9d;
        }
    }

    public static void main(String[] args) {
        JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory();
        schemaFactory.setAutoPutDollarSchema(true);
        JsonNode schema = schemaFactory.createSchema(BadClass.class);

        System.err.println(schema);
    }
}

The exception is

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:658)
    at com.github.reinert.jjschema.v1.PropertyWrapper.firstToLowerCase(PropertyWrapper.java:352)
    at com.github.reinert.jjschema.v1.PropertyWrapper.processPropertyName(PropertyWrapper.java:347)
    at com.github.reinert.jjschema.v1.PropertyWrapper.getName(PropertyWrapper.java:170)
    at com.github.reinert.jjschema.v1.PropertyWrapper.<init>(PropertyWrapper.java:103)
    at com.github.reinert.jjschema.v1.CustomSchemaWrapper.processProperties(CustomSchemaWrapper.java:155)
    at com.github.reinert.jjschema.v1.CustomSchemaWrapper.<init>(CustomSchemaWrapper.java:88)
    at com.github.reinert.jjschema.v1.CustomSchemaWrapper.<init>(CustomSchemaWrapper.java:64)
    at com.github.reinert.jjschema.v1.SchemaWrapperFactory.createWrapper(SchemaWrapperFactory.java:72)
    at com.github.reinert.jjschema.v1.SchemaWrapperFactory.createWrapper(SchemaWrapperFactory.java:48)
    at com.github.reinert.jjschema.v1.SchemaWrapperFactory.createWrapper(SchemaWrapperFactory.java:40)
    at com.github.reinert.jjschema.v1.JsonSchemaV4Factory.createSchema(JsonSchemaV4Factory.java:36)
    at SchemaGenerator.main(SchemaGenerator.java:16)

If the problem can't be fixed, I expect execution to terminate with an exception explaining that get() is a bad method name, or the getter to be silently ignored (possibly with a log message?).

ehues commented 6 years ago

I notice that https://github.com/reinert/JJSchema/issues/70 has a similar stack trace, but I don't know how the reporter solved their problem.

reinert commented 6 years ago

I just solved this case. Do you need a new release? Please let me know.

ehues commented 6 years ago

Thanks for the prompt fix!

I don't need a release immediately.