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

[sdk-gen] Use specialized missing required property exception #1228

Merged
merged 3 commits into from
Oct 13, 2023

Conversation

Zaid-Ajaj
Copy link
Contributor

Description

This PR adds a new custom exception MissingRequiredPropertyException to the main Java SDK. Then SDK-gen uses this type and throws it as an error when an input property is required but isn't specified. This fixes #1199 where previously the users would receive a NullPointerException instead.

Due to using Go templates and conditionals as follows:

{{ if $setter.IsRequired }} 
...
{{ end }}

The generated code emits an empty line when IsRequired is false which is why you will see the new line diffs in the commits of this PR

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • Yes, there are changes in this PR that warrants bumping the Pulumi Service API version

@Zaid-Ajaj Zaid-Ajaj added area/codegen Code generation area/sdks SDKs labels Sep 27, 2023
@Zaid-Ajaj Zaid-Ajaj requested a review from a team September 27, 2023 15:09
if prop.IsRequired() && prop.DefaultValue == nil && prop.ConstValue == nil {
fprintf(w, " if ($.%s == null) {\n", fieldName)
fprintf(w, " throw new %s(\"%s\", \"%s\");\n",
ctx.ref(names.PulumiMissingRequiredPropertyException), pt.name, fieldName)
Copy link
Member

Choose a reason for hiding this comment

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

Are we going to need to first ship the exception in the core Java SDK, and then somehow have the codegen indicate that version of the core Java SDK is now required (since it depends on this new exception being there)? I'm not familiar enough with Java on how to indicate that requirement.

Copy link
Member

Choose a reason for hiding this comment

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

Yes we do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought it should be fine because if it is merged and tag a release, a new sdk is published that includes the new exception type, sometime later providers team will pick up the newer sdk-gen from pulumi-java and start using that new type. This is assuming that sdk-gen references latest pulumi-java sdk

Copy link
Member

Choose a reason for hiding this comment

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

sdk-gen dictates the version of the core library to use. So we need to release the type first, then make sure sdk-gen is setting that version as the required version when emiting the provider sdks.

Copy link
Member

Choose a reason for hiding this comment

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

i.e. "This is assuming that sdk-gen references latest pulumi-java sdk" is the assumption that doesn't hold. e.g. this line which will be set from in https://github.com/pulumi/pulumi-java/blob/main/pkg/codegen/java/templates_gradle.go#L87 somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i.e. "This is assuming that sdk-gen references latest pulumi-java sdk" is the assumption that doesn't hold.

The pulumi-java-gen CLI used to generate java SDKs takes the version of the pulumi-java main sdk as input but it defaults to the version of the pulumi-java-gen itself, see here:

cmd.Flags().StringVar(&javaSdkVersionArg, "sdk", version.Version,
	"com.pulumi:pulumi version to depend on in the generated code")

Doesn't that mean if we tag 0.9.x which publishes both SDK and generator, that the generated SDKs will be using 0.9.x as well? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

What about from the Pulumi cli where we call the gen methods directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tested what Pulumi CLI does when you run:

pulumi package gen-sdk schema.json --language java --out sdk

and it generates just java source files without a gradle project file 😱 so basically gen-sdk isn't usable with java projects

Copy link
Member

Choose a reason for hiding this comment

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

🤦‍♂️ alright, well we'll need to look into that at some point but means nothing to worry about for this PR

Zaid-Ajaj added a commit that referenced this pull request Oct 3, 2023
# Description

So that it can be used from
#1228 to fix #1199

## Checklist

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have updated the
[CHANGELOG-PENDING](https://github.com/pulumi/pulumi/blob/master/CHANGELOG_PENDING.md)
file with my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Service,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Service API version
<!-- @pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
@Zaid-Ajaj Zaid-Ajaj force-pushed the use-specialized-missing-required-property-exception branch from 7932305 to 8196203 Compare October 6, 2023 11:59
@Zaid-Ajaj Zaid-Ajaj modified the milestone: 0.95 Oct 6, 2023
@Zaid-Ajaj Zaid-Ajaj merged commit fe67fea into main Oct 13, 2023
@Zaid-Ajaj Zaid-Ajaj deleted the use-specialized-missing-required-property-exception branch October 13, 2023 16:37
EronWright added a commit to pulumi/pulumi-kubernetes that referenced this pull request May 24, 2024
<!--Thanks for your contribution. See [CONTRIBUTING](CONTRIBUTING.md)
    for Pulumi's contribution guidelines.

    Help us merge your changes more quickly by adding more details such
    as labels, milestones, and reviewers.-->

### Proposed changes

This PR updates the pulumi-java dependency from v0.8.0 to v0.12.0.

Notable changes:
- pulumi/pulumi-java#1356
- pulumi/pulumi-java#1361
- pulumi/pulumi-java#1228

### Related issues (optional)

<!--Refer to related PRs or issues: #1234, or 'Fixes #1234' or 'Closes
#1234'.
Or link to full URLs to issues or pull requests in other GitHub
repositories. -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/codegen Code generation area/sdks SDKs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java SDK panics when missing required property
3 participants