joelittlejohn / jsonschema2pojo

Generate Java types from JSON or JSON Schema and annotate those types for data-binding with Jackson, Gson, etc
http://www.jsonschema2pojo.org
Apache License 2.0
6.24k stars 1.66k forks source link

Avoid NPE when setting additionalProperties path #1515

Closed MikeEdgar closed 1 year ago

MikeEdgar commented 1 year ago

A schema's id is not guaranteed to be non-null. E.g.

https://github.com/joelittlejohn/jsonschema2pojo/blob/80d827db93e4cc3848c4f297ebbfe4b3fd936272/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java#L89-L91

andreaTP commented 1 year ago

any update on this fix?

joelittlejohn commented 1 year ago

Thanks for pinging @andreaTP. I saw the CI job failed and didn't get around to investigating that. I've kicked this off again and will merge if this passes.

MikeEdgar commented 1 year ago

I think the issue with CI may be this: https://github.blog/changelog/2022-08-09-github-actions-the-ubuntu-18-04-actions-runner-image-is-being-deprecated-and-will-be-removed-by-12-1-22/. Would you like me to bump the Ubuntu version to 20.04 or 22.04 as part of this PR?

unkish commented 1 year ago

Would it be possible to provide also test case(s) as outlined in https://github.com/joelittlejohn/jsonschema2pojo/blob/master/CONTRIBUTING.md

MikeEdgar commented 1 year ago

Unit test added. Between the new test and the existing integration tests, both null/non-null schema Ids are tested.

joelittlejohn commented 1 year ago

@MikeEdgar is it not possible to recreate this case via the integration tests?

MikeEdgar commented 1 year ago

@joelittlejohn , the integration tests all seem to be using a particular approach where the schemas are loaded from JSON (which always sets the id) using the Maven plugin Mojo. If that's the preferred location for new tests, it will likely need to be an alternate process.

MikeEdgar commented 1 year ago

@joelittlejohn , can this PR move forward as-is or are additional testing changes necessary? IMO the scope of the change itself is quite minor from a testing risk perspective and a unit test has been added of course.

andreaTP commented 1 year ago

Ping @joelittlejohn and @unkish , anything that should be done here to get the fix merged and released?

Thanks in advance 🙏

unkish commented 1 year ago

anything that should be done here to get the fix merged and released?

Release schedule & content is up to maintainer to decide (might be dependent on available free time/resources/...)

MikeEdgar commented 1 year ago

@joelittlejohn , @unkish - please have another look and let me know if any other changes are necessary.

unkish commented 1 year ago

From the change perspective itself: it looks perfectly fine and valid. There is also similar "guard" in ArrayRule https://github.com/joelittlejohn/jsonschema2pojo/blob/98c85ce4d4b9333bafae2c18eca5637ea6a7c5da/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/rules/ArrayRule.java#L84

Latter was added with commit https://github.com/joelittlejohn/jsonschema2pojo/commit/8e97ddb03a82bd366250392c3e79450c6420bd6a

\ Additionally verified that NPE would also be thrown via public API "usage" in SchemaMapper and current change would fix it:

public class SchemaMapperTest {
    ...

    @Test
    public void generateCreatesTypeFromSchemaWithAdditionalProperties() throws Exception {
        String schema = "{\"type\":\"object\",\"additionalProperties\":{\"type\":\"integer\",\"maximum\":9}}";
        JType result = new SchemaMapper().generate(new JCodeModel(), "ExampleClass", "", schema);
        JType returnType = ((JDefinedClass) result).getMethod("getAdditionalProperties", new JType[0]).type();

        assertEquals("Map<String,Integer>", returnType.name());
    }

}