Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed failing unknown call for array schema #174

Merged
merged 1 commit into from
Apr 20, 2020
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
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Tape Current File",
"program": "${workspaceFolder}/node_modules/tape/bin/tape",
"args": [
"${file}"
],
"console": "internalConsole"
}
]
}
6 changes: 4 additions & 2 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const create = function (options = {}) {
let schema;

if ((parameter.in === 'body' || parameter.in === 'formData') && parameter.schema) {
schema = enjoi.schema(parameter.schema).unknown(allowUnknownProperties);
schema = enjoi.schema(parameter.schema);
if (schema.schemaType === 'object') {
schema = schema.unknown(allowUnknownProperties);
}
}
else {
const template = {
Expand Down Expand Up @@ -96,7 +99,6 @@ const create = function (options = {}) {
},
validate: async function (value) {
const data = coerce && value && coerce(value);
console.log('validation data: %j', data);
const result = await schema.validate(data);

if (result.error) {
Expand Down
91 changes: 87 additions & 4 deletions test/test-hapi-openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ Test('test plugin', function (t) {
handlers: Path.join(__dirname, './fixtures/handlers'),
docs: {
path: '/spec',
prefixBasePath: false
prefixBasePath: false
}
}
});
Expand Down Expand Up @@ -759,8 +759,8 @@ Test('test plugin', function (t) {
}
});

t.test('parse description from api definition', async function(t) {
t.test('do not break with empty descriptions', async function(t) {
t.test('parse description from api definition', async function (t) {
t.test('do not break with empty descriptions', async function (t) {
t.plan(1);

const server = new Hapi.Server();
Expand Down Expand Up @@ -804,7 +804,7 @@ Test('test plugin', function (t) {
}
});

t.test('create the right description for the route', async function(t) {
t.test('create the right description for the route', async function (t) {
t.plan(1);

const server = new Hapi.Server();
Expand Down Expand Up @@ -1003,6 +1003,89 @@ Test('test plugin', function (t) {

});

t.test('hapi array parameters', async function (t) {
t.plan(1);

const server = new Hapi.Server();

const api = {
swagger: '2.0',
info: {
title: 'Minimal',
version: '1.0.0'
},
paths: {
'/test': {
post: {
parameters: [
{
name: 'body',
in: 'body',
schema: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string"
},
breed: {
type: "string"
}
}
}
}
}
],
responses: {
200: {
description: 'default response'
}
}
}
}
}
};

try {
await server.register({
plugin: OpenAPI,
options: {
api,
handlers: {
test: {
post() {
return 'test';
}
}
}
}
});

let response = await server.inject({
method: 'POST',
url: '/test',
payload: [
{
name: 'Fido',
breed: 'Pointer'
},
{
name: 'Frodo',
breed: 'Beagle'
}
]
});

t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`);

}
catch (error) {
t.fail(error.message);
}

});

t.test('hapi operation tags', async function (t) {
t.plan(1);

Expand Down