Skip to content

Commit e232143

Browse files
authored
Merge pull request #3 from JaredCE/deal-with-models-correctly
Deal with models correctly
2 parents c2a766e + 9bb0c14 commit e232143

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,11 @@ This will end up with the original OpenAPI document containing a new property se
127127
}
128128
}
129129
```
130+
131+
You can then use the AWS CLI v2 command to upload the documentation parts:
132+
133+
```bash
134+
aws apigateway import-documentation-parts --rest-api-id your-rest-api-id --body fileb://openAPI.json
135+
```
136+
137+
This asusmes that you have saved the augmented OpenAPI to a file called **openAPI.json** and that you know your rest-api-id.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-aws-decorator",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"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 ",
55
"main": "index.js",
66
"keywords": [

src/DocumentationParts.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
class DocumentationParts {
44
constructor(openAPI) {
55
this.openAPI = openAPI;
6+
this.newModelNames = {};
7+
this.oldModelNames = {};
68
}
79

810
parse() {
911
let parts = [];
1012
const apiPart = this.__createAPIPart();
1113
parts.push(apiPart);
1214

15+
const modelParts = this.__manipulateModels();
16+
parts = parts.concat(modelParts);
17+
1318
const resourceParts = this.__createPathParts();
1419
parts = parts.concat(resourceParts);
15-
// parts = [...resourceParts, ...apiPart];
20+
1621
return parts;
1722
}
1823

@@ -23,7 +28,7 @@ class DocumentationParts {
2328
type: "API",
2429
},
2530
properties: {
26-
...info,
31+
info,
2732
},
2833
};
2934
return part;
@@ -66,8 +71,13 @@ class DocumentationParts {
6671
for (const method of Object.keys(this.pathValue)) {
6772
const description = this.pathValue[method]?.description;
6873
const summary = this.pathValue[method]?.summary;
74+
6975
parts.push({
70-
location: { type: "METHOD", path: this.pathName, method: method },
76+
location: {
77+
type: "METHOD",
78+
path: this.pathName,
79+
method: method,
80+
},
7181
properties: {
7282
...(description && { description: description }),
7383
...(summary && { summary: summary }),
@@ -160,6 +170,44 @@ class DocumentationParts {
160170

161171
return parts;
162172
}
173+
174+
__manipulateModels() {
175+
const parts = [];
176+
if (this.openAPI?.components?.schemas) {
177+
for (const [key, model] of Object.entries(
178+
this.openAPI.components.schemas
179+
)) {
180+
const newKey = key.replace(/[\W_]+/g, "");
181+
Object.assign(this.oldModelNames, { [key]: newKey });
182+
Object.assign(this.newModelNames, { [newKey]: key });
183+
184+
Object.assign(this.openAPI.components.schemas, {
185+
[newKey]: this.openAPI.components.schemas[key],
186+
});
187+
188+
if (newKey !== key) delete this.openAPI.components.schemas[key];
189+
190+
const part = this.__createMODELPart(model, newKey);
191+
192+
parts.push(part);
193+
}
194+
}
195+
196+
return parts;
197+
}
198+
199+
__createMODELPart(model, key) {
200+
const part = {
201+
location: {
202+
type: "MODEL",
203+
name: key,
204+
},
205+
properties: {
206+
schema: model,
207+
},
208+
};
209+
return part;
210+
}
163211
}
164212

165213
module.exports = DocumentationParts;

test/unit/OpenAPIDecorator.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe(`OpenAPIDecorator`, function () {
3838
});
3939

4040
expect(APIPart).lengthOf(1);
41-
expect(APIPart[0].properties).to.be.eql(openAPIDoc.info);
41+
expect(APIPart[0].properties.info).to.be.eql(openAPIDoc.info);
4242
});
4343

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

195195
expect(APIPart).lengthOf(5);
196196
});
197+
198+
it(`generates MODEL location part for each component schema on an openAPI document`, function () {
199+
const openAPIDoc = JSON.parse(JSON.stringify(validOpenAPI));
200+
const openAPIDecorator = new OpenAPIDecorator(openAPIDoc);
201+
openAPIDecorator.decorate();
202+
203+
expect(openAPIDecorator.openAPI).to.have.property(
204+
"x-amazon-apigateway-documentation"
205+
);
206+
207+
expect(
208+
openAPIDecorator.openAPI["x-amazon-apigateway-documentation"]
209+
).to.have.property("documentationParts");
210+
211+
const APIPart = openAPIDecorator.openAPI[
212+
"x-amazon-apigateway-documentation"
213+
].documentationParts.filter((part) => {
214+
if (part.location.type === "MODEL") return part;
215+
});
216+
217+
expect(APIPart).lengthOf(1);
218+
});
197219
});
198220
});

0 commit comments

Comments
 (0)