Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid NPE when setting additionalProperties path #1515

Merged

Conversation

MikeEdgar
Copy link
Contributor

@MikeEdgar MikeEdgar commented Apr 25, 2023

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

return ruleFactory.getSchemaRule().apply(className, schemaNode, null, jpackage, new Schema(null, schemaNode, null));

@andreaTP
Copy link

andreaTP commented May 9, 2023

any update on this fix?

@joelittlejohn
Copy link
Owner

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
Copy link
Contributor Author

MikeEdgar commented May 9, 2023

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
Copy link
Collaborator

unkish commented May 9, 2023

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

@MikeEdgar MikeEdgar force-pushed the additionalproperties-npe branch from 84b51d4 to 7f9af17 Compare May 11, 2023 20:14
@MikeEdgar
Copy link
Contributor Author

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

@MikeEdgar MikeEdgar force-pushed the additionalproperties-npe branch from a6b22ad to 3b9c181 Compare May 15, 2023 17:19
@joelittlejohn
Copy link
Owner

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

@MikeEdgar
Copy link
Contributor Author

@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
Copy link
Contributor Author

@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
Copy link

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

Thanks in advance 🙏

@unkish
Copy link
Collaborator

unkish commented Jun 1, 2023

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 MikeEdgar force-pushed the additionalproperties-npe branch from 3b9c181 to 7bd063c Compare June 1, 2023 13:34
@MikeEdgar
Copy link
Contributor Author

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

@unkish
Copy link
Collaborator

unkish commented Jun 28, 2023

From the change perspective itself: it looks perfectly fine and valid. There is also similar "guard" in ArrayRule

if (schema.getId() == null || schema.getId().getFragment() == null) {

Latter was added with commit 8e97ddb


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());
    }

}

@joelittlejohn joelittlejohn merged commit 1e51d0f into joelittlejohn:master Jul 8, 2023
@joelittlejohn joelittlejohn added this to the 1.2.2 milestone Jul 8, 2023
@MikeEdgar MikeEdgar deleted the additionalproperties-npe branch July 8, 2023 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants