Skip to content

Commit

Permalink
Merge pull request #3 from JaredCE/deal-with-models-correctly
Browse files Browse the repository at this point in the history
Deal with models correctly
  • Loading branch information
JaredCE authored Jun 25, 2023
2 parents c2a766e + 9bb0c14 commit e232143
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,11 @@ This will end up with the original OpenAPI document containing a new property se
}
}
```

You can then use the AWS CLI v2 command to upload the documentation parts:

```bash
aws apigateway import-documentation-parts --rest-api-id your-rest-api-id --body fileb://openAPI.json
```

This asusmes that you have saved the augmented OpenAPI to a file called **openAPI.json** and that you know your rest-api-id.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi-aws-decorator",
"version": "0.0.1",
"version": "0.0.2",
"description": "This decorates OpenAPI 3.0.X files with AWS APIGateway OpenAPI tags: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-documenting-api-content-representation.html ",
"main": "index.js",
"keywords": [
Expand Down
54 changes: 51 additions & 3 deletions src/DocumentationParts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
class DocumentationParts {
constructor(openAPI) {
this.openAPI = openAPI;
this.newModelNames = {};
this.oldModelNames = {};
}

parse() {
let parts = [];
const apiPart = this.__createAPIPart();
parts.push(apiPart);

const modelParts = this.__manipulateModels();
parts = parts.concat(modelParts);

const resourceParts = this.__createPathParts();
parts = parts.concat(resourceParts);
// parts = [...resourceParts, ...apiPart];

return parts;
}

Expand All @@ -23,7 +28,7 @@ class DocumentationParts {
type: "API",
},
properties: {
...info,
info,
},
};
return part;
Expand Down Expand Up @@ -66,8 +71,13 @@ class DocumentationParts {
for (const method of Object.keys(this.pathValue)) {
const description = this.pathValue[method]?.description;
const summary = this.pathValue[method]?.summary;

parts.push({
location: { type: "METHOD", path: this.pathName, method: method },
location: {
type: "METHOD",
path: this.pathName,
method: method,
},
properties: {
...(description && { description: description }),
...(summary && { summary: summary }),
Expand Down Expand Up @@ -160,6 +170,44 @@ class DocumentationParts {

return parts;
}

__manipulateModels() {
const parts = [];
if (this.openAPI?.components?.schemas) {
for (const [key, model] of Object.entries(
this.openAPI.components.schemas
)) {
const newKey = key.replace(/[\W_]+/g, "");
Object.assign(this.oldModelNames, { [key]: newKey });
Object.assign(this.newModelNames, { [newKey]: key });

Object.assign(this.openAPI.components.schemas, {
[newKey]: this.openAPI.components.schemas[key],
});

if (newKey !== key) delete this.openAPI.components.schemas[key];

const part = this.__createMODELPart(model, newKey);

parts.push(part);
}
}

return parts;
}

__createMODELPart(model, key) {
const part = {
location: {
type: "MODEL",
name: key,
},
properties: {
schema: model,
},
};
return part;
}
}

module.exports = DocumentationParts;
24 changes: 23 additions & 1 deletion test/unit/OpenAPIDecorator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe(`OpenAPIDecorator`, function () {
});

expect(APIPart).lengthOf(1);
expect(APIPart[0].properties).to.be.eql(openAPIDoc.info);
expect(APIPart[0].properties.info).to.be.eql(openAPIDoc.info);
});

it(`generates RESOURCE location part for each resource on an openAPI document`, function () {
Expand Down Expand Up @@ -194,5 +194,27 @@ describe(`OpenAPIDecorator`, function () {

expect(APIPart).lengthOf(5);
});

it(`generates MODEL location part for each component schema on an openAPI document`, function () {
const openAPIDoc = JSON.parse(JSON.stringify(validOpenAPI));
const openAPIDecorator = new OpenAPIDecorator(openAPIDoc);
openAPIDecorator.decorate();

expect(openAPIDecorator.openAPI).to.have.property(
"x-amazon-apigateway-documentation"
);

expect(
openAPIDecorator.openAPI["x-amazon-apigateway-documentation"]
).to.have.property("documentationParts");

const APIPart = openAPIDecorator.openAPI[
"x-amazon-apigateway-documentation"
].documentationParts.filter((part) => {
if (part.location.type === "MODEL") return part;
});

expect(APIPart).lengthOf(1);
});
});
});

0 comments on commit e232143

Please sign in to comment.