Skip to content

Commit 39472b5

Browse files
authored
Merge pull request #271 from JaredCE/fix-contentType-examples
Fix content type examples
2 parents 288d74c + 940e2e6 commit 39472b5

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

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": "serverless-openapi-documenter",
3-
"version": "0.0.123",
3+
"version": "0.0.124",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

src/definitionGenerator.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ class DefinitionGenerator {
713713
}
714714

715715
if (contentKey) {
716-
717716
const obj = {};
718717
let schema;
719718
if (mediaTypeDocumentation.content) {
@@ -734,7 +733,9 @@ class DefinitionGenerator {
734733
}
735734

736735
if (mediaTypeDocumentation.examples) {
737-
obj.example = mediaTypeDocumentation.examples;
736+
obj.examples = this.createExamples(
737+
mediaTypeDocumentation.examples
738+
);
738739
}
739740

740741
schema = mediaTypeDocumentation.schema;

test/unit/definitionGenerator.spec.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const path = require("path");
99

1010
const serverlessMock = require("../helpers/serverless");
1111
const modelsDocument = require("../models/models/models.json");
12+
const alternativeModelsDocument = require("../models/models/models-alt.json");
1213

1314
const schemaHandler = require('../../src/schemaHandler');
1415

@@ -1254,5 +1255,89 @@ describe("DefinitionGenerator", () => {
12541255
expect(response["200"].headers).to.have.property("x-rate-limit");
12551256
});
12561257
});
1258+
1259+
describe(`mediaTypeObjects`, function () {
1260+
describe(`content style`, function () {
1261+
it(`should add examples when examples are specified`, async function () {
1262+
const modelsWithExample = structuredClone(modelsDocument);
1263+
modelsWithExample.models.at(0).content["application/json"].examples = [{ name: '404Error', summary: 'an example of a 404 error', description: 'This is what a 404 error looks like', value: '404' }]
1264+
Object.assign(mockServerless.service.custom.documentation, modelsWithExample);
1265+
1266+
const description = "this is a description";
1267+
const responseMock = {
1268+
methodResponses: [
1269+
{
1270+
responseBody: { description: description },
1271+
responseModels: { 'application/json': 'ErrorResponse' },
1272+
statusCode: 200,
1273+
},
1274+
],
1275+
};
1276+
1277+
const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/ErrorResponse')
1278+
1279+
const definitionGenerator = new DefinitionGenerator(
1280+
mockServerless,
1281+
logger
1282+
);
1283+
1284+
const response = await definitionGenerator.createResponses(responseMock);
1285+
1286+
expect(response).to.be.an("object");
1287+
expect(response).to.have.property("200");
1288+
expect(response["200"]).to.have.property('content')
1289+
expect(response["200"].content).to.be.an('object')
1290+
expect(response["200"].content).to.have.property('application/json')
1291+
expect(response["200"].content['application/json']).to.be.an('object')
1292+
expect(response["200"].content['application/json']).to.have.property('examples')
1293+
expect(response["200"].content['application/json'].examples).to.be.an('object')
1294+
expect(response["200"].content['application/json'].examples).to.have.property('404Error')
1295+
1296+
1297+
stub.restore()
1298+
});
1299+
});
1300+
1301+
describe(`contentType style`, function () {
1302+
it(`should add examples when examples are specified`, async function () {
1303+
const altModelsWithExample = structuredClone(alternativeModelsDocument);
1304+
altModelsWithExample.models.at(0).examples = [{ name: '404Error', summary: 'an example of a 404 error', description: 'This is what a 404 error looks like', value: '404' }]
1305+
Object.assign(mockServerless.service.custom.documentation, altModelsWithExample);
1306+
1307+
const description = "this is a description";
1308+
const responseMock = {
1309+
methodResponses: [
1310+
{
1311+
responseBody: { description: description },
1312+
responseModels: { 'application/json': 'ErrorResponse' },
1313+
statusCode: 200,
1314+
},
1315+
],
1316+
};
1317+
1318+
const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/ErrorResponse')
1319+
1320+
const definitionGenerator = new DefinitionGenerator(
1321+
mockServerless,
1322+
logger
1323+
);
1324+
1325+
1326+
const response = await definitionGenerator.createResponses(responseMock);
1327+
1328+
expect(response).to.be.an("object");
1329+
expect(response).to.have.property("200");
1330+
expect(response["200"]).to.have.property('content')
1331+
expect(response["200"].content).to.be.an('object')
1332+
expect(response["200"].content).to.have.property('application/json')
1333+
expect(response["200"].content['application/json']).to.be.an('object')
1334+
expect(response["200"].content['application/json']).to.have.property('examples')
1335+
expect(response["200"].content['application/json'].examples).to.be.an('object')
1336+
expect(response["200"].content['application/json'].examples).to.have.property('404Error')
1337+
1338+
stub.restore()
1339+
});
1340+
});
1341+
});
12571342
});
12581343
});

0 commit comments

Comments
 (0)