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

fix(cfn2ts): doesn't handle property types with the same type as a primitive type #25460

Merged
merged 3 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions tools/@aws-cdk/cfn2ts/lib/genspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,18 @@ export function isPrimitive(type: CodeName): boolean {
|| type.className === 'Date';
}

function specTypeToCodeType(resourceContext: CodeName, type: string): CodeName {
/**
* @param resourceContext
* @param type the name of the type
* @param complexType whether the type is a complexType (true) or primitive type (false)
*/
function specTypeToCodeType(resourceContext: CodeName, type: string, complexType: boolean): CodeName {
if (type.endsWith('[]')) {
const itemType = specTypeToCodeType(resourceContext, type.slice(0, -2));
const itemType = specTypeToCodeType(resourceContext, type.slice(0, -2), complexType);
return CodeName.forPrimitive(`${itemType.className}[]`);
}
if (schema.isPrimitiveType(type)) {
return specPrimitiveToCodePrimitive(type);
if (!complexType) {
return specPrimitiveToCodePrimitive(type as schema.PrimitiveType);
} else if (type === 'Tag') {
// Tags are not considered primitive by the CloudFormation spec (even though they are intrinsic)
// so we won't consider them primitive either.
Expand All @@ -301,9 +306,12 @@ function specTypeToCodeType(resourceContext: CodeName, type: string): CodeName {

/**
* Translate a list of type references in a resource context to a list of code names
*
* @param resourceContext
* @param types name and whether the type is a complex type (true) or primitive type (false)
*/
export function specTypesToCodeTypes(resourceContext: CodeName, types: string[]): CodeName[] {
return types.map(type => specTypeToCodeType(resourceContext, type));
export function specTypesToCodeTypes(resourceContext: CodeName, types: { [name: string]: boolean }): CodeName[] {
return Object.entries(types).map(([name, complexType]) => specTypeToCodeType(resourceContext, name, complexType));
}

export interface PropertyVisitor<T> {
Expand Down
28 changes: 22 additions & 6 deletions tools/@aws-cdk/cfn2ts/lib/spec-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ export class PropertyAttributeName extends SpecName {
}

/**
* Return a list of all allowable item types (either primitive or complex).
* Return a list of all allowable item types, separating out primitive and complex
* types because sometimes a complex type can have the same name as a primitive type.
* If we only return the names in this case then the primitive type will always override
* the complex type (not what we want).
*
* @returns type name and whether the type is a complex type (true) or primitive type (false)
*/
export function itemTypeNames(spec: schema.CollectionProperty): string[] {
return complexItemTypeNames(spec).concat(primitiveItemTypeNames(spec));
export function itemTypeNames(spec: schema.CollectionProperty): { [name: string]: boolean } {
const types = complexItemTypeNames(spec).map(type => [type, true])
.concat(primitiveItemTypeNames(spec).map(type => [type, false]));

return Object.fromEntries(types);
}

function complexItemTypeNames(spec: schema.CollectionProperty): string[] {
Expand All @@ -98,10 +106,18 @@ function primitiveItemTypeNames(spec: schema.CollectionProperty): string[] {
}

/**
* Return a list of all allowable types (either primitive or complex).
* Return a list of all allowable item types, separating out primitive and complex
* types because sometimes a complex type can have the same name as a primitive type.
* If we only return the names in this case then the primitive type will always override
* the complex type (not what we want).
*
* @returns type name and whether the type is a complex type (true) or primitive type (false)
*/
export function scalarTypeNames(spec: schema.ScalarProperty): string[] {
return complexScalarTypeNames(spec).concat(primitiveScalarTypeNames(spec));
export function scalarTypeNames(spec: schema.ScalarProperty): { [name: string]: boolean } {
const types = complexScalarTypeNames(spec).map(type => [type, true] )
.concat(primitiveScalarTypeNames(spec).map(type => [type, false]));

return Object.fromEntries(types);
}

function complexScalarTypeNames(spec: schema.ScalarProperty): string[] {
Expand Down