Skip to content

Conversation

@sar9ho
Copy link

@sar9ho sar9ho commented Dec 21, 2025

Adds a “required fields” constructor for Java types when generateJSpecifyAnnotations = true.

Behavior:

For testing, I added some targeted cases in CodeGenTest ^

Fixes #900
Fixes #901

@sjohnr sjohnr self-assigned this Jan 6, 2026
Copy link
Contributor

@sjohnr sjohnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @sar9ho! This looks like a good start, but I have some suggestions for changes below.

Comment on lines +470 to +474
if (fields.isNotEmpty() && fields.size < 256) {
addParameterizedConstructor(fields, javaType)
}

addParameterizedConstructor(requiredFields, javaType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change would be better included under the existing javaGenerateAllConstructor check, so that generating one (or more) multi-arg constructors is still tied to that configuration item. It's not actually necessary to link the required-args constructor to JSpecify (generateJSpecifyAnnotations).

Alternatively, I am wondering if it might be better to add a new config item (javaGenerateRequiredFieldsConstructor) to control adding this new constructor instead of automatically adding it.

In either case, there should also be a check for the required-fields constructor to ensure it's not added if all fields are required (fields.size == requiredFields.size). It would be good to include a test for this case as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants