Skip to content

Commit

Permalink
Fix: fixes request body for swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed Jan 20, 2025
1 parent c5924d1 commit ad7337a
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 112 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
],
"rules": {
"more/no-hardcoded-password": 0,
"unicorn/catch-error-name": 0
"unicorn/catch-error-name": 0,
"unicorn/text-encoding-identifier-case": 0
},
"overrides": [{
"files": "tests/examples/*test.js",
Expand Down
10 changes: 10 additions & 0 deletions examples/blog/documentation/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
"example": "application/json"
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "string",
"example": "{\"data\":{\"title\":\"quite cotton although shadow\",\"body\":\"mood income built field throw badly finest seat accurate ago seldom allow invented exactly past garage baseball\",\"thumbnail\":\"http://lakkik.ng/kupa\"}}"
}
}
}
},
"responses": {
"201": {
"description": "Positive: create blog post",
Expand Down
74 changes: 37 additions & 37 deletions examples/chat/app.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import jsonServer from 'json-server';

const port = process.env.PORT || 3001;
const users = [ {
'id' : 1,
'name' : 'Leigh',
'email' : 'helvy0@feedburner.com'
}, {
'id' : 2,
'name' : 'Ancell',
'email' : 'pancell1@gravatar.com'
}, {
'id' : 3,
'name' : 'Conre',
'email' : 'lconre2@ezinearticles.com'
} ];

const messages = [ {
text : 'butter property president flow nodded degree where keep',
sender : 2
}, {
text : 'cheese tried dig interior watch tone time train living',
sender : 1
} ];

const server = jsonServer.create();
const router = jsonServer.router({ users, messages });

server.use(router);

if (!process.env.TEST) {
server.listen(port, () => {
console.log(`Chat server is running on ${port}`);
});
}

export default server;
import jsonServer from 'json-server';

const port = process.env.PORT || 3001;
const users = [ {
'id' : 1,
'name' : 'Leigh',
'email' : 'helvy0@feedburner.com'
}, {
'id' : 2,
'name' : 'Ancell',
'email' : 'pancell1@gravatar.com'
}, {
'id' : 3,
'name' : 'Conre',
'email' : 'lconre2@ezinearticles.com'
} ];

const messages = [ {
text : 'butter property president flow nodded degree where keep',
sender : 2
}, {
text : 'cheese tried dig interior watch tone time train living',
sender : 1
} ];

const server = jsonServer.create();
const router = jsonServer.router({ users, messages });

server.use(router);

if (!process.env.TEST) {
server.listen(port, () => {
console.log(`Chat server is running on ${port}`);
});
}

export default server;
7 changes: 0 additions & 7 deletions examples/chat/documentation/api-blueprint.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,11 @@ FORMAT: 1A
+ Headers

AUTH: 5NM2p40Z8
Content-Type: application/json

+ Parameters

sender: 2
limit: 5

+ Body

{
"name": "McCoy"
}

+ Response 200 (application/json)

Expand Down
26 changes: 18 additions & 8 deletions examples/chat/documentation/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@
"example": "application/json"
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"example": {
"name": "McCoy"
},
"properties": {
"name": {
"type": "string",
"example": "McCoy"
}
}
}
}
}
},
"responses": {
"200": {
"description": "Positive: change user name",
Expand Down Expand Up @@ -125,14 +143,6 @@
"type": "string"
},
"example": "5NM2p40Z8"
},
{
"name": "Content-Type",
"in": "header",
"schema": {
"type": "string"
},
"example": "application/json"
}
],
"responses": {
Expand Down
107 changes: 53 additions & 54 deletions examples/chat/test.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
import chronicle, { supertest } from '../../src';
import app from './app';

const request = supertest(app);

function contextBuilder({ test }) {
return {
title : test.title,
group : test.parent.title
};
}

before(async () => {
chronicle.setContextBuilder(contextBuilder);
});

suite('Users');
test('Positive: show user profile', async function () {
await request
.with(this)
.get('/users/:id')
.params({ id: 1 })
.expect('Content-Type', /json/)
.expect(200);
});

test('Positive: change user name', async function () {
await request
.with(this)
.patch('/users/:id')
.params({ id: 2 })
.set({ 'AUTH': '5NM2p40Z8' })
.send({ name: 'McCoy' })
.expect('Content-Type', /json/)
.expect(200);
});

suite('Messages');

test('Positive: get list of messages', async function () {
await request
.with(this)
.get('/messages?sender=2&limit=5')
.set({ 'AUTH': '5NM2p40Z8' })
.send({ name: 'McCoy' })
.expect('Content-Type', /json/)
.expect(200);
});

after(async () => {
await chronicle.save('./documentation/swagger.json', { reporter: 'swagger' });
await chronicle.save('./documentation/api-blueprint.md', { reporter: 'api-blueprint' });
chronicle.clear();
});
import chronicle, { supertest } from '../../src';
import app from './app';

const request = supertest(app);

function contextBuilder({ test }) {
return {
title : test.title,
group : test.parent.title
};
}

before(async () => {
chronicle.setContextBuilder(contextBuilder);
});

suite('Users');
test('Positive: show user profile', async function () {
await request
.with(this)
.get('/users/:id')
.params({ id: 1 })
.expect('Content-Type', /json/)
.expect(200);
});

test('Positive: change user name', async function () {
await request
.with(this)
.patch('/users/:id')
.params({ id: 2 })
.set({ 'AUTH': '5NM2p40Z8' })
.send({ name: 'McCoy' })
.expect('Content-Type', /json/)
.expect(200);
});

suite('Messages');

test('Positive: get list of messages', async function () {
await request
.with(this)
.get('/messages?sender=2&limit=5')
.set({ 'AUTH': '5NM2p40Z8' })
.expect('Content-Type', /json/)
.expect(200);
});

after(async () => {
await chronicle.save('./documentation/swagger.json', { reporter: 'swagger' });
await chronicle.save('./documentation/api-blueprint.md', { reporter: 'api-blueprint' });
chronicle.clear();
});
9 changes: 8 additions & 1 deletion src/modules/Action.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ export default class Action {
return this._request._method || 'GET';
}

get reqContentInfo() {
return {
type : 'application/json',
charset : 'utf-8'
};
}

get reqHeaders() {
return Action.sanitizeHeaders(this._request.headers, this._chronicle.config.headers?.request);
}
Expand All @@ -179,6 +186,7 @@ export default class Action {
if (!url) return null;
const request = {
...url,
info : this.reqContentInfo,
method : this.method,
headers : this.reqHeaders
};
Expand Down Expand Up @@ -214,7 +222,6 @@ export default class Action {
get resContentInfo() {
return {
type : this.info?.type || 'application/json',
// eslint-disable-next-line unicorn/text-encoding-identifier-case
charset : this.info?.charset || 'utf-8'
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/reporters/Swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export default class SwaggerReporter extends Base {
parameters : [
...this._renderHeaders(request.headers)
],
requestBody : request.body ? {
content : {
[request.info.type] : {
schema : this._renderBody(request.body)
}
}
} : undefined,
responses : {
[response.status.code] : {
description : title,
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/examples-blog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ before(async function () {


test('blog', async function () {
this.timeout(60*1000)
this.timeout(60 * 1000);
await execSync(`npx ava --config ${path.join(exampleFolder, '/ava.config.js ')}`, {
env : {
...process.env,
Expand Down
33 changes: 33 additions & 0 deletions tests/mock/fixtures/reports/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@ export const createUserAction = {
'example' : '25NPmT'
}
],
'requestBody' : {
'content' : {
'application/json' : {
'schema' : {
'type' : 'object',
'example' : {
'first_name' : 'Pascal',
'last_name' : 'Ancell',
'email' : 'pancell1@gravatar.com',
'gender' : 'Male'
},
'properties' : {
'first_name' : {
'type' : 'string',
'example' : 'Pascal'
},
'last_name' : {
'type' : 'string',
'example' : 'Ancell'
},
'email' : {
'type' : 'string',
'example' : 'pancell1@gravatar.com'
},
'gender' : {
'type' : 'string',
'example' : 'Male'
}
}
}
}
}
},
'responses' : {
'200' : {
'description' : 'create user',
Expand Down
7 changes: 6 additions & 1 deletion tests/package/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ test('Manually setting required attributes', function () {
path : '/api/users',
query : { status: 'ACTIVE' },
method : 'GET',
headers : null
headers : null,
'info' : {
'charset' : 'utf-8',
'type' : 'application/json'
}

});

assert.deepOwnInclude(response, {
Expand Down
6 changes: 5 additions & 1 deletion tests/package/seed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ test('POST request', async function () {
query : {},
method : 'POST',
headers : { Authorization: '25NPmT' },
body : {
'info' : {
'charset' : 'utf-8',
'type' : 'application/json'
},
body : {
'first_name' : 'Pascal',
'last_name' : 'Ancell',
'email' : 'pancell1@gravatar.com',
Expand Down
Loading

0 comments on commit ad7337a

Please sign in to comment.