From 7268a52c60333b05a0d3bafb05af0b1de36d07bf Mon Sep 17 00:00:00 2001 From: Praneet Loke <1466314+praneetloke@users.noreply.github.com> Date: Thu, 16 May 2024 20:15:24 -0700 Subject: [PATCH] Ensure enum names are prefixed with the resource name from the context (#151) --- pkg/openapi.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/openapi.go b/pkg/openapi.go index 121c836..8c8e425 100644 --- a/pkg/openapi.go +++ b/pkg/openapi.go @@ -760,6 +760,33 @@ func (o *OpenAPIContext) gatherResourceProperties(resourceName string, requestBo addNameOverride(propName, sdkName, o.apiToSDKNameMap) } + // If the cloud API nests the response inside a property + // using the name of the resource, it'll certainly cause + // a failure in generating the .NET SDK but we don't set + // the Pulumi languageOverride for them because it doesn't + // make sense from an end-user perspective to nest the output + // inside another property. It lends to a bad dev UX. + // So providers should actually pluck the nested property + // in both the OpenAPI spec, as well as in the provider + // callbacks. + // + // For example, say, you have a resource called `Database` + // and the response from the API looks like this: + // ```json + // { + // "database":{...} + // } + // ``` + // If left unmodified, then the Pulumi resource would + // have an output property called `database` in addition + // to all the input properties also being outputs. + // That means, the user would get this: + // ```typescript + // const db = new Database("db", {someProp:""}); <-- someProp could be an input + // db.database.someProp; <-- someProp is returned by the API in response to create the resource, which is normal. + // db.someProp; <-- input property again accessible as output. + // ``` + // Don't add `id` to the output properties since Pulumi // automatically adds that via `CustomResource` which // is what all resources in the SDK will extend. @@ -1322,6 +1349,9 @@ func (ctx *resourceContext) genEnumType(enumName string, propSchema openapi3.Sch } typName := ToPascalCase(enumName) + if !strings.HasPrefix(typName, ctx.resourceName) { + typName = ctx.resourceName + enumName + } tok := fmt.Sprintf("%s:%s:%s", ctx.pkg.Name, ctx.mod, typName) enumSpec := &pschema.ComplexTypeSpec{