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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"test": "npm run just-test-in-node && npm run lint-errors",
"test-in-node": "npm run lint-errors && npm run just-test-in-node",
"just-test": "karma start --config karma.conf.js",
"just-test-in-node": "NODE_ENV=test mocha --recursive --compilers js:babel-core/register test",
"just-test-in-node": "cross-env NODE_ENV=test mocha --recursive --compilers js:babel-core/register test",
"just-test-in-node-watch": "npm run just-test-in-node -- -w",
"serve-static": "http-server -i -a 0.0.0.0 -p 3001",
"prestart": "npm install",
Expand Down Expand Up @@ -77,6 +77,7 @@
"babel-preset-stage-0": "^6.22.0",
"babel-runtime": "^6.23.0",
"brace": "^0.10.0",
"cross-env": "^5.1.3",
"css-loader": "0.22.0",
"deep-extend": "^0.4.1",
"deepmerge": "^1.3.2",
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/editor-autosuggest/spec-selectors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSelector } from "reselect"
import { Set, Map } from "immutable"
import { escapeJsonPointerToken } from "../refs-util"
Copy link
Author

@jemerald jemerald Feb 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shockey Is this the preferred way for sharing code? Or should it go through the magic plugin system?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine to me - the plugin system is great for exposing code that someone else may want to override/customize, but I can't imagine someone wanting to write a custom JSON Pointer escaper 😄


const SWAGGER2_REF_MAP = {
"paths": "pathitems",
Expand Down Expand Up @@ -52,7 +53,7 @@ export const localRefs = (state) => (sys) => createSelector(
.map( name => Map({
name,
type,
$ref: `#/${type}/${name}`,
$ref: `#/${type}/${escapeJsonPointerToken(name)}`,
}))
})
}
Expand Down
13 changes: 2 additions & 11 deletions src/plugins/jump-to-path/spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { unescapeJsonPointerToken } from "../refs-util"

export default function spec() {
return {
statePlugins: {
Expand Down Expand Up @@ -73,14 +75,3 @@ function jsonPointerToArray(pointer) {

return pointer.split("/").map(unescapeJsonPointerToken)
}

/**
* Unescapes a JSON pointer.
* @api public
*/
function unescapeJsonPointerToken(token) {
if (typeof token !== "string") {
return token
}
return token.replace(/~1/g, "/").replace(/~0/g, "~")
}
18 changes: 18 additions & 0 deletions src/plugins/refs-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Unescapes a JSON pointer.
* @api public
*/
export function unescapeJsonPointerToken(token) {
if (typeof token !== "string") {
return token
}
return token.replace(/~1/g, "/").replace(/~0/g, "~")
}

/**
* Escapes a JSON pointer.
* @api public
*/
export function escapeJsonPointerToken(token) {
return token.replace(/~/g, "~0").replace(/\//g, "~1")
}
2 changes: 2 additions & 0 deletions src/plugins/validate-semantic/validators/refs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import get from "lodash/get"
import { escapeJsonPointerToken } from "../../refs-util"

export const validateRefHasNoSiblings = () => system => {
return system.validateSelectors.all$refs()
Expand Down Expand Up @@ -34,6 +35,7 @@ export const validateUnusedDefinitions = () => (system) => {

system.specSelectors.definitions()
.forEach((val, key) => {
key = escapeJsonPointerToken(key)
if(references.indexOf(`#/definitions/${key}`) < 0) {
const path = ["definitions", key]
errors.push({
Expand Down
37 changes: 37 additions & 0 deletions test/plugins/validate-semantic/refs.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,43 @@ describe("validation plugin - semantic - refs", function() {
})
})

it("should not return a warning when a definition with special character is declared and used", () => {
const spec = {
paths: {
"/CoolPath": {
get: {
responses: {
200: {
schema: {
$ref: "#/definitions/x~1Foo"
}
},
400: {
schema: {
$ref: "#/definitions/x~0Bar"
}
}
}
}
}
},
definitions: {
"x/Foo": {
type: "object"
},
"x~Bar": {
type: "object"
}
}
}

return validateHelper(spec)
.then(system => {
const allErrors = system.errSelectors.allErrors().toJS()
expect(allErrors.length).toEqual(0)
})
})

})
describe("Malformed $ref values", () => {
it("should return an error when a JSON pointer lacks a leading `#/`", () => {
Expand Down