Repository: glennjones/hapi-swagger (js)

Issue:
Invalid swagger produced
#### Context

* *hapi-swgger version*: 7.7.0
* *any other relevant information*:

#### What are you trying to achieve or the steps to reproduce ?

Invalid swagger generated from conditionally required joi operation parameters. Create a hapi path like this:

```js
const ... = {
  method: '...',
  path: '...',
  config: {
    description: '...',
    tags: ['api'],
    validate: {
      query: {
        nonce: Joi.string()
          .meta(cli.newParamMetaData('n', ['nonce']))
          .description('A nonce')
          .when('response_type', {
            is: /^id_token( token)?$/,
            then: Joi.required(),
          }),
        ...
      },
    },
    plugins: {
      ...
    },
  },
  handler: ...
}
```

#### What result did you get?

```
{
            "type": "string",
            "description": "A nonce",
            "x-meta": {
              "flag": "n",
              "aliases": [
                "nonce"
              ]
            },
            "required": [ 
              "nonce"
            ],
            "x-alternatives": [
              {
                "type": "string",
                "description": "A nonce",
                "x-meta": {
                  "flag": "n",
                  "aliases": [
                    "nonce"
                  ]
                }
              }
            ],
            "name": "nonce",
            "in": "query"
          },
```

#### What did you expect ?

```
{
            "type": "string",
            "description": "A nonce",
            "x-meta": {
              "flag": "n",
              "aliases": [
                "nonce"
              ]
            },
            "required": true, // <---- THIS
            "x-alternatives": [
              {
                "type": "string",
                "description": "A nonce",
                "x-meta": {
                  "flag": "n",
                  "aliases": [
                    "nonce"
                  ]
                }
              }
            ],
            "name": "nonce",
            "in": "query"
          },
```

Pull request description:
fixes #413

Patch:
diff --git a/lib/parameters.js b/lib/parameters.js
index 95ed377..f1f09bc 100644
--- a/lib/parameters.js
+++ b/lib/parameters.js
@@ -94,7 +94,7 @@ parameters.fromProperties = function(schemaObj, parameterType) {
             item.in = parameterType;
 
             // reinstate required at parameter level
-            if (schemaObj.required && schemaObj.required.indexOf(key) > -1) {
+            if (schemaObj.required && (schemaObj.properties[key].required || schemaObj.required.indexOf(key) > -1)) {
                 item.required = true;
             }
             if (schemaObj.optional && schemaObj.optional.indexOf(key) > -1) {


Test patch:
diff --git a/test/Integration/responses-tests.js b/test/Integration/responses-tests.js
index 9662278..1f73d1d 100644
--- a/test/Integration/responses-tests.js
+++ b/test/Integration/responses-tests.js
@@ -134,7 +134,45 @@ lab.experiment('responses', () => {
         });
     });
 
-    lab.test('using hapi response.schema with child objects', done => {
+
+    lab.test('conditional variables produce `required = true`, not `required = [...]`', (done) => {
+
+        const routes = {
+            method: 'POST',
+            path: '/store/',
+            config: {
+                handler: Helper.defaultHandler,
+                tags: ['api'],
+                validate: {
+                    query: {
+                        nonce: Joi.string()
+                        .when('response_type', {
+                            is: /^id_token( token)?$/,
+                            then: Joi.required(),
+                        }),
+                        response_type: Joi.string()
+                        .allow('code', 'id_token token', 'id_token')
+                        .required()
+                    },
+                },
+            }
+        };
+
+        Helper.createServer({}, routes, (err, server) => {
+
+            server.inject({ url: '/swagger.json' }, function (response) {
+
+                expect(err).to.equal(null);
+                //console.log(JSON.stringify(response.result));
+                expect(response.result.paths['/store/'].post.parameters[0].required).to.equal(true);
+                Helper.validate(response, done, expect);
+            });
+        });
+    });
+
+
+    lab.test('using hapi response.schema with child objects', (done) => {
+
         const routes = {
             method: 'POST',
             path: '/store/',