Skip to content
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
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": "serverless-openapi-documenter",
"version": "0.0.123",
"version": "0.0.124",
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
"main": "index.js",
"keywords": [
Expand Down
5 changes: 3 additions & 2 deletions src/definitionGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,6 @@ class DefinitionGenerator {
}

if (contentKey) {

const obj = {};
let schema;
if (mediaTypeDocumentation.content) {
Expand All @@ -734,7 +733,9 @@ class DefinitionGenerator {
}

if (mediaTypeDocumentation.examples) {
obj.example = mediaTypeDocumentation.examples;
obj.examples = this.createExamples(
mediaTypeDocumentation.examples
);
}

schema = mediaTypeDocumentation.schema;
Expand Down
85 changes: 85 additions & 0 deletions test/unit/definitionGenerator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require("path");

const serverlessMock = require("../helpers/serverless");
const modelsDocument = require("../models/models/models.json");
const alternativeModelsDocument = require("../models/models/models-alt.json");

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

Expand Down Expand Up @@ -1254,5 +1255,89 @@ describe("DefinitionGenerator", () => {
expect(response["200"].headers).to.have.property("x-rate-limit");
});
});

describe(`mediaTypeObjects`, function () {
describe(`content style`, function () {
it(`should add examples when examples are specified`, async function () {
const modelsWithExample = structuredClone(modelsDocument);
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' }]
Object.assign(mockServerless.service.custom.documentation, modelsWithExample);

const description = "this is a description";
const responseMock = {
methodResponses: [
{
responseBody: { description: description },
responseModels: { 'application/json': 'ErrorResponse' },
statusCode: 200,
},
],
};

const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/ErrorResponse')

const definitionGenerator = new DefinitionGenerator(
mockServerless,
logger
);

const response = await definitionGenerator.createResponses(responseMock);

expect(response).to.be.an("object");
expect(response).to.have.property("200");
expect(response["200"]).to.have.property('content')
expect(response["200"].content).to.be.an('object')
expect(response["200"].content).to.have.property('application/json')
expect(response["200"].content['application/json']).to.be.an('object')
expect(response["200"].content['application/json']).to.have.property('examples')
expect(response["200"].content['application/json'].examples).to.be.an('object')
expect(response["200"].content['application/json'].examples).to.have.property('404Error')


stub.restore()
});
});

describe(`contentType style`, function () {
it(`should add examples when examples are specified`, async function () {
const altModelsWithExample = structuredClone(alternativeModelsDocument);
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' }]
Object.assign(mockServerless.service.custom.documentation, altModelsWithExample);

const description = "this is a description";
const responseMock = {
methodResponses: [
{
responseBody: { description: description },
responseModels: { 'application/json': 'ErrorResponse' },
statusCode: 200,
},
],
};

const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/ErrorResponse')

const definitionGenerator = new DefinitionGenerator(
mockServerless,
logger
);


const response = await definitionGenerator.createResponses(responseMock);

expect(response).to.be.an("object");
expect(response).to.have.property("200");
expect(response["200"]).to.have.property('content')
expect(response["200"].content).to.be.an('object')
expect(response["200"].content).to.have.property('application/json')
expect(response["200"].content['application/json']).to.be.an('object')
expect(response["200"].content['application/json']).to.have.property('examples')
expect(response["200"].content['application/json'].examples).to.be.an('object')
expect(response["200"].content['application/json'].examples).to.have.property('404Error')

stub.restore()
});
});
});
});
});