Skip to content

Commit

Permalink
Fix NodeJS metadata type (#159)
Browse files Browse the repository at this point in the history
All of our languages except NodeJS use
`SchemaPackageWithObjectMetaType`. As a result, Node exposes only input
types for object metadata which makes it awkward to consume downstream.

I'm assuming #143 didn't change
this for backwards-compatibility reasons, or maybe it was overlooked. I
_think_ it is safe to continue generating the `ObjectMeta` type alias
(in case users are importing it) but use the input/output types
(matching the upstream k8s types) on resources.

Edit: 

> // schemaPackageWithObjectMetaType is the Pulumi schema package used to
> // generate code for languages that need an ObjectMeta type (Python, Go, and .NET)

So this seems intentional. For whatever reason, using this with Node has
the effect of generating input/output types matching upstream's types,
which seems desirable.

Fixes #158
  • Loading branch information
blampe authored Nov 13, 2024
1 parent 7a5693d commit d77b74b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## Unreleased
## 1.5.4 (2024-11-13)

- NodeJS now uses correct input/output types for object metadata. (https://github.com/pulumi/crd2pulumi/issues/158)

## 1.5.3 (2024-09-30)

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type ObjectMetaPatch = k8s.types.input.meta.v1.ObjectMetaPatch;
`

func GenerateNodeJS(pg *PackageGenerator, name string) (map[string]*bytes.Buffer, error) {
pkg := pg.SchemaPackage()
pkg := pg.SchemaPackageWithObjectMetaType()
oldName := pkg.Name
pkg.Name = name

Expand Down
23 changes: 23 additions & 0 deletions tests/crds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,29 @@ func TestKubernetesVersionNodeJs(t *testing.T) {
execCrd2Pulumi(t, "nodejs", "crds/k8sversion/mock_crd.yaml", validateVersion)
}

func TestNodeJsObjectMeta(t *testing.T) {
validateVersion := func(t *testing.T, path string) {
// enter and build the generated package
withDir(t, path, func() {
runRequireNoError(t, exec.Command("npm", "install"))
runRequireNoError(t, exec.Command("npm", "run", "build"))

filename := filepath.Join(path, "k8sversion", "test", "testResource.ts")
t.Logf("validating objectmeta type in %s", filename)

testResource, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("expected to read generated NodeJS code: %s", err)
}

assert.Contains(t, string(testResource), "public readonly metadata!: pulumi.Output<outputs.meta.v1.ObjectMeta>;", "expected metadata output type")
assert.Contains(t, string(testResource), "metadata?: pulumi.Input<inputs.meta.v1.ObjectMeta>;", "expected metadata input type")
})
}

execCrd2Pulumi(t, "nodejs", "crds/k8sversion/mock_crd.yaml", validateVersion)
}

func withDir(t *testing.T, dir string, f func()) {
pwd, err := os.Getwd()
require.NoError(t, err)
Expand Down

0 comments on commit d77b74b

Please sign in to comment.