Repository: glennjones/hapi-swagger (js)

Issue:
$ref values must be RFC3986-compliant percent-encoded URIs
# Issue
After extracting `swagger.json` using the `hapi-swagger` and using the <https://editor.swagger.io> ,
I got reciving next error
```
type: Semantic error at paths... $ref values must be RFC3986-compliant percent-encoded URIs
```

## Environment

```
npx: installed 1 in 1.599s

  Binaries:
    Node: 11.1.0 - /usr/local/bin/node
    Yarn: 1.12.1 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
  npmPackages:
    hapi: ^18.1.0 => 18.1.0 
    hapi-auth-jwt2: ^8.6.1 => 8.6.1 
    hapi-swagger: ^10.0.1 => 10.0.1 
```

## Expected Behavior
The problem is due to the space contained in the automatically generated model name.
ex) Model 1, Model 2

## Actual Behavior
that's it

## Reproducible Demo
just extracting `swagger.json` and paste that to <https://editor.swagger.io>


Pull request description:
fix: $ref must be  RFC3986 compliant. Closes #594

Patch:
diff --git a/lib/definitions.js b/lib/definitions.js
index c4e1023..945f3f5 100644
--- a/lib/definitions.js
+++ b/lib/definitions.js
@@ -83,7 +83,7 @@ internals.append = function(definitionName, definition, currentCollection, force
     }
     currentCollection[out] = definition;
   }
-  return out;
+  return encodeURIComponent(out);
 };
 
 /**


Test patch:
diff --git a/test/Integration/alternatives-test.js b/test/Integration/alternatives-test.js
index a5f11c4..6c60460 100644
--- a/test/Integration/alternatives-test.js
+++ b/test/Integration/alternatives-test.js
@@ -288,7 +288,7 @@ lab.experiment('alternatives', () => {
         in: 'body',
         name: 'body',
         schema: {
-          $ref: '#/definitions/Model 1'
+          $ref: '#/definitions/Model%201'
         }
       }
     ]);
diff --git a/test/Integration/child-model.js b/test/Integration/child-model.js
index 143af02..3c4b001 100644
--- a/test/Integration/child-model.js
+++ b/test/Integration/child-model.js
@@ -102,7 +102,7 @@ lab.experiment('child-models', () => {
     expect(response.statusCode).to.equal(200);
 
     expect(response.result.paths['/foo/v1/bar'].post.parameters[0].schema).to.equal({
-      $ref: '#/definitions/Model 1'
+      $ref: '#/definitions/Model%201'
     });
 
     expect(response.result.definitions['Model 1']).to.equal({
diff --git a/test/Integration/definitions-test.js b/test/Integration/definitions-test.js
index 194ba9b..0ef5871 100644
--- a/test/Integration/definitions-test.js
+++ b/test/Integration/definitions-test.js
@@ -102,7 +102,7 @@ lab.experiment('definitions', () => {
 
     expect(response.statusCode).to.equal(200);
     expect(response.result.paths['/test/'].post.parameters[0].schema).to.equal({
-      $ref: '#/definitions/Model 1'
+      $ref: '#/definitions/Model%201'
     });
     expect(response.result.definitions['Model 1']).to.equal(defination);
     const isValid = await Validate.test(response.result);
diff --git a/test/Integration/joifunctions-test.js b/test/Integration/joifunctions-test.js
index 12ed103..02966f2 100644
--- a/test/Integration/joifunctions-test.js
+++ b/test/Integration/joifunctions-test.js
@@ -56,7 +56,7 @@ lab.experiment('validation', () => {
         in: 'body',
         name: 'body',
         schema: {
-          $ref: '#/definitions/Hidden Model'
+          $ref: '#/definitions/Hidden%20Model'
         }
       }
     ]);
diff --git a/test/Integration/path-test.js b/test/Integration/path-test.js
index 36a05bb..6a2d45c 100644
--- a/test/Integration/path-test.js
+++ b/test/Integration/path-test.js
@@ -576,7 +576,7 @@ lab.experiment('path', () => {
         in: 'body',
         name: 'body',
         schema: {
-          $ref: '#/definitions/Model 1'
+          $ref: '#/definitions/Model%201'
         }
       }
     ]);
diff --git a/test/Integration/proxy-test.js b/test/Integration/proxy-test.js
index 739575f..669e9e5 100644
--- a/test/Integration/proxy-test.js
+++ b/test/Integration/proxy-test.js
@@ -197,7 +197,7 @@ lab.experiment('proxies', () => {
         in: 'body',
         name: 'body',
         schema: {
-          $ref: '#/definitions/Model 1'
+          $ref: '#/definitions/Model%201'
         }
       }
     ]);
diff --git a/test/Integration/responses-tests.js b/test/Integration/responses-tests.js
index d7aab03..be18a15 100644
--- a/test/Integration/responses-tests.js
+++ b/test/Integration/responses-tests.js
@@ -873,19 +873,19 @@ lab.experiment('responses', () => {
             },
             '400': {
               schema: {
-                $ref: '#/definitions/Model 1'
+                $ref: '#/definitions/Model%201'
               },
               description: '400 - Added from plugin-options'
             },
             '404': {
               description: '404 from response status object',
               schema: {
-                $ref: '#/definitions/Model 1'
+                $ref: '#/definitions/Model%201'
               }
             },
             '429': {
               schema: {
-                $ref: '#/definitions/Model 1'
+                $ref: '#/definitions/Model%201'
               },
               description: 'Too Many Requests'
             },
diff --git a/test/unit/property-test.js b/test/unit/property-test.js
index 07d912f..3b12b7b 100644
--- a/test/unit/property-test.js
+++ b/test/unit/property-test.js
@@ -65,7 +65,7 @@ lab.experiment('property - ', () => {
       format: 'binary'
     });
     expect(propertiesNoAlt.parseProperty('x', Joi.array(), null, 'body', true, false)).to.equal({
-      $ref: '#/definitions/Model 1'
+      $ref: '#/definitions/Model%201'
     });
     expect(propertiesNoAlt.parseProperty('x', Joi.any(), null, 'body', true, false)).to.equal({ type: 'string' });
     //expect(propertiesNoAlt.parseProperty('x', Joi.func(), null, 'body', true, false)).to.equal({ 'type': 'string' });