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: SAM spec type is wrong for Serverless Function Policies #636

Merged
merged 1 commit into from
Oct 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ export class SAMSpecImporter extends ResourceSpecImporterBase<SAMResourceSpecifi
protected deriveType(spec: resourcespec.SAMTypeDefinition): PropertyType {
const self = this;

// Slight interpretation hack: if `Inclusive[Primitive]ItemTypes` are present,
// we need to honor them even if "List" is not present in the types. So
// if we detect them, add "List" to the `Types` array.
if (spec.InclusiveItemTypes || spec.InclusivePrimitiveItemTypes) {
if (spec.Type !== 'List' && !(spec.Types ?? []).includes('List')) {
spec = {
...spec,
Types: [...(spec.Types ?? []), 'List'],
};
}
}

return maybeUnion([
...(spec.PrimitiveTypes ?? []).map(primitiveType),
...(spec.Type ? [namedType(spec.Type)] : []),
Expand Down
79 changes: 79 additions & 0 deletions packages/@aws-cdk/service-spec-importers/test/sam-spec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { emptyDatabase } from '@aws-cdk/service-spec-types';
import { SAMSpecImporter } from '../src/importers/import-resource-spec';

/**
* Tests for the CloudFormation spec of SAM
*/

let db: ReturnType<typeof emptyDatabase>;
beforeEach(() => {
db = emptyDatabase();
});

test('respect InclusivePrimitiveItemTypes even if List is not given', () => {
SAMSpecImporter.importTypes({
db,
specification: {
ResourceSpecificationVersion: '2016-10-31',
ResourceSpecificationTransform: 'AWS::Serverless-2016-10-31',
ResourceTypes: {
'AWS::Some::Type': {
Properties: {
SomeParameter: {
UpdateType: 'Mutable',
PrimitiveTypes: ['String'],
Types: ['Type1'],
InclusivePrimitiveItemTypes: ['Integer'],
InclusiveItemTypes: ['Type2'],
},
},
},
},
PropertyTypes: {
'AWS::Some::Type.Type1': {
Properties: {
Field: { UpdateType: 'Mutable', PrimitiveType: 'String' },
},
},
'AWS::Some::Type.Type2': {
Properties: {
Field: { UpdateType: 'Mutable', PrimitiveType: 'String' },
},
},
},
},
});

const resource = db.lookup('resource', 'cloudFormationType', 'equals', 'AWS::Some::Type').only();
expect(resource.properties.SomeParameter.type).toEqual({
type: 'union',
types: [
{
type: 'string',
},
{
reference: {
$ref: '2',
},
type: 'ref',
},
{
element: {
type: 'union',
types: [
{
type: 'integer',
},
{
reference: {
$ref: '3',
},
type: 'ref',
},
],
},
type: 'array',
},
],
});
});
Loading