Skip to content

cloudfront: Go documentation examples for optional array fields cause compilation errors #5036

@umekikazuya

Description

@umekikazuya

Describe the issue

The code examples in the official CDK Go documentation fail to compile when copied directly.

This occurs with optional array fields, where there is a type mismatch in the generated examples.

Details:

  • When a TypeScript property is defined as an optional array (e.g., removeHeaders?: string[]), jsii correctly generates the Go type as
    *[]*string (pointer to a slice)
  • However, the documentation examples show code that assigns []*string (a slice) directly to this field, which causes a type mismatch and compilation failure

This affects multiple packages across CDK, including:

  • @aws-cdk/aws-cloudfront - ResponseHeadersPolicyProps.RemoveHeaders
  • @aws-cdk/aws-certificatemanager - CertificateProps.SubjectAlternativeNames

Example from CloudFront documentation

TypeScript source:

 /**
  * A list of HTTP response headers that CloudFront removes from HTTP responses
  * that it sends to viewers.
  *
  * @default - no headers are removed
  */
 readonly removeHeaders?: string[];

Generated Go type:

RemoveHeaders *[]*string `field:"optional" json:"removeHeaders" yaml:"removeHeaders"`

Go Example Code:

//   	RemoveHeaders: []*string{
//   		jsii.String("Server"),
//   	},

Compilation error:

cannot use []*string{...} (value of type []*string) as type *[]*string in struct literal

Working code (not shown in documentation):

// Option 1: Take address of slice literal
myResponseHeadersPolicy := cloudfront.NewResponseHeadersPolicy(this, jsii.String("ResponseHeadersPolicy"), &ResponseHeadersPolicyProps{
   RemoveHeaders: &[]*string{jsii.String("Server")},
})

// Option 2: Use intermediate variable
headers := []*string{jsii.String("Server")}
myResponseHeadersPolicy := cloudfront.NewResponseHeadersPolicy(this, jsii.String("ResponseHeadersPolicy"), &ResponseHeadersPolicyProps{
   RemoveHeaders: &headers,
})

This appears to be a Rosetta (TypeScript-to-Go translation tool) issue. The generated examples don't account for the fact that *[]*string requires a pointer to a slice, not a slice directly.

Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationThis is a problem with documentation.effort/mediumMedium work item – a couple days of effortlanguage/goRegarding GoLang bindingsp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions