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 Swagger serialization #34

Merged
merged 1 commit into from
Sep 9, 2017
Merged
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
53 changes: 47 additions & 6 deletions api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,47 @@ interface SwaggerSpec {
"x-amazon-apigateway-binary-media-types"?: string[];
}

// jsonStringifySwaggerSpec creates a JSON string out of a Swagger spec object. This is required versus an
// ordinary JSON.stringify because the spec contains computed values.
function jsonStringifySwaggerSpec(spec: SwaggerSpec): { json: string | fabric.Computed<string>, hash: string } {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious - why does this need to support the case of returning a string instead of just always returning a fabric.Computed<string>? Not that it really matters assuming we undo all of this after pulumi/pulumi#331.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no methods, there would be no computeds, and because Property isn't public there's no way to create a zero initialized one without resorting to internal runtime APIs. Obviously if we kept any of this, we'd need to fix this, but as you say, we aren't.

let last: fabric.Computed<void> | undefined;
let pathValues: {[path: string]: {[method: string]: SwaggerOperation} } = {};
for (let path of Object.keys(spec.paths)) {
pathValues[path] = {};
for (let method of Object.keys(spec.paths[path])) {
// Set up a callback to remember the final value, and chain it on the previous one.
let resolvePathValue: fabric.Computed<void> =
spec.paths[path][method].mapValue((op: SwaggerOperation) => { pathValues[path][method] = op; });
last = last ? last.mapValue(() => resolvePathValue) : resolvePathValue;
}
}

let promptSpec = {
swagger: spec.swagger,
info: spec.info,
paths: pathValues,
"x-amazon-apigateway-binary-media-types": spec["x-amazon-apigateway-binary-media-types"],
};

// Produce a hash of all the promptly available values.
// BUGBUG[pulumi/pulumi-fabric#331]: we are skipping hashing of the actual operation objects, because they
// are possibly computed, and we need the hash promptly for resource URN creation. This isn't correct,
// and will lead to hash collisions; we need to fix this as part of fixing pulumi/pulumi-fabric#331
let promptHash: string = sha1hash(JSON.stringify(promptSpec));

// After all values have settled, we can produce the resulting string.
if (last) {
return {
json: last.mapValue(() => JSON.stringify(Object.assign({}, promptSpec, { paths: pathValues }))),
hash: promptHash,
};
}
return {
json: JSON.stringify(promptSpec),
hash: promptHash,
};
}

interface SwaggerInfo {
title: string;
version: string;
Expand Down Expand Up @@ -390,15 +431,14 @@ export class HttpAPI {
}

public publish(): fabric.Computed<string> {
let swaggerJSON = JSON.stringify(this.swaggerSpec);
let { json, hash } = jsonStringifySwaggerSpec(this.swaggerSpec);
this.api = new aws.apigateway.RestApi(this.apiName, {
body: swaggerJSON,
body: json,
});
let deploymentId = sha1hash(swaggerJSON);
this.deployment = new aws.apigateway.Deployment(this.apiName + "_" + deploymentId, {
this.deployment = new aws.apigateway.Deployment(this.apiName + "_" + hash, {
restApi: this.api,
stageName: "",
description: "Deployment of version " + deploymentId,
description: "Deployment of version " + hash,
});
let stage = new aws.apigateway.Stage(this.apiName + "_stage", {
stageName: stageName,
Expand All @@ -413,7 +453,8 @@ export class HttpAPI {
if (lambda !== undefined) {
if (method === "x-amazon-apigateway-any-method") {
method = "*";
} else {
}
else {
method = method.toUpperCase();
}
let permissionName = this.apiName + "_invoke_" + sha1hash(method + path);
Expand Down