Skip to content

Commit f982eb4

Browse files
authored
Merge pull request #4049 from shockey/support/editor-validation-refactor
Supporting changes for Swagger-Editor semantic validation changes
2 parents 9c2f9d2 + 10b4f5d commit f982eb4

File tree

16 files changed

+110
-71
lines changed

16 files changed

+110
-71
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
[
1010
"module-alias",
1111
[
12+
{
13+
"expose": "root",
14+
"src": "."
15+
},
1216
{
1317
"expose": "components",
1418
"src": "src/core/components"

docs/customization/plugin-api.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ A plugin return value may contain any of these keys, where `myStateKey` is a nam
1919
},
2020
components: {},
2121
wrapComponents: {},
22-
fn: {}
22+
afterLoad: (system) => {}
23+
fn: {},
2324
}
2425
```
2526

@@ -363,7 +364,35 @@ const MyWrapComponentPlugin = function(system) {
363364
}
364365
```
365366

367+
##### `afterLoad`
366368

369+
The `afterLoad` plugin method allows you to get a reference to the system after your plugin has been registered with the system.
370+
371+
This interface is used in the core code to attach methods that are driven by bound selectors or actions directly to the system.
372+
373+
```javascript
374+
const MyMethodProvidingPlugin = function() {
375+
return {
376+
afterLoad(system) {
377+
// at this point in time, your actions have been bound into the system
378+
// so you can do things with them
379+
system.myMethod = system.exampleActions.updateFavoriteColor
380+
},
381+
statePlugins: {
382+
example: {
383+
actions: {
384+
updateFavoriteColor: (str) => {
385+
return {
386+
type: "EXAMPLE_SET_FAV_COLOR",
387+
payload: str
388+
}
389+
}
390+
}
391+
}
392+
}
393+
}
394+
}
395+
```
367396

368397
##### fn
369398

src/core/index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ module.exports = function SwaggerUI(opts) {
9292
}, constructorConfig.initialState)
9393
}
9494

95+
if(constructorConfig.initialState) {
96+
// if the user sets a key as `undefined`, that signals to us that we
97+
// should delete the key entirely.
98+
// known usage: Swagger-Editor validate plugin tests
99+
for (var key in constructorConfig.initialState) {
100+
if(
101+
constructorConfig.initialState.hasOwnProperty(key)
102+
&& constructorConfig.initialState[key] === undefined
103+
) {
104+
delete storeConfigs.state[key]
105+
}
106+
}
107+
}
108+
95109
let inlinePlugin = ()=> {
96110
return {
97111
fn: constructorConfig.fn,
@@ -105,8 +119,6 @@ module.exports = function SwaggerUI(opts) {
105119

106120
var system = store.getSystem()
107121

108-
system.initOAuth = system.authActions.configureAuth
109-
110122
const downloadSpec = (fetchedConfig) => {
111123
if(typeof constructorConfig !== "object") {
112124
return system
@@ -138,6 +150,9 @@ module.exports = function SwaggerUI(opts) {
138150
} else if(mergedConfig.dom_id) {
139151
let domNode = document.querySelector(mergedConfig.dom_id)
140152
system.render(domNode, "App")
153+
} else if(mergedConfig.dom_id === null || mergedConfig.domNode === null) {
154+
// do nothing
155+
// this is useful for testing that does not need to do any rendering
141156
} else {
142157
console.error("Skipped rendering: no `dom_id` or `domNode` was specified")
143158
}

src/core/plugins/auth/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import * as specWrapActionReplacements from "./spec-wrap-actions"
55

66
export default function() {
77
return {
8+
afterLoad(system) {
9+
system.initOAuth = system.authActions.configureAuth
10+
},
811
statePlugins: {
912
auth: {
1013
reducers,
File renamed without changes.

src/plugins/configs/index.js renamed to src/core/plugins/configs/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import YAML from "js-yaml"
2-
import yamlConfig from "../../../swagger-config.yaml"
2+
import yamlConfig from "root/swagger-config.yaml"
33
import * as actions from "./actions"
44
import * as selectors from "./selectors"
55
import reducers from "./reducers"
File renamed without changes.
File renamed without changes.
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
import reduce from "lodash/reduce"
2-
let request = require.context("./transformers/", true, /\.js$/)
3-
let errorTransformers = []
2+
import * as NotOfType from "./transformers/not-of-type"
3+
import * as ParameterOneOf from "./transformers/parameter-oneof"
4+
import * as StripInstance from "./transformers/strip-instance"
45

5-
request.keys().forEach( function( key ){
6-
if( key === "./hook.js" ) {
7-
return
8-
}
9-
10-
if( !key.match(/js$/) ) {
11-
return
12-
}
13-
14-
if( key.slice(2).indexOf("/") > -1) {
15-
// skip files in subdirs
16-
return
17-
}
18-
19-
errorTransformers.push({
20-
name: toTitleCase(key).replace(".js", "").replace("./", ""),
21-
transform: request(key).transform
22-
})
23-
})
6+
const errorTransformers = [
7+
NotOfType,
8+
ParameterOneOf,
9+
StripInstance
10+
]
2411

2512
export default function transformErrors (errors, system) {
2613
let inputs = {
@@ -47,10 +34,3 @@ export default function transformErrors (errors, system) {
4734
})
4835

4936
}
50-
51-
function toTitleCase(str) {
52-
return str
53-
.split("-")
54-
.map(substr => substr[0].toUpperCase() + substr.slice(1))
55-
.join("")
56-
}

src/core/plugins/spec/actions.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ export const resolveSpec = (json, url) => ({specActions, specSelectors, errActio
105105
errActions.clear({
106106
type: "thrown"
107107
})
108-
109-
if(errors.length > 0) {
108+
if(Array.isArray(errors) && errors.length > 0) {
110109
let preparedErrors = errors
111110
.map(err => {
112111
console.error(err)

0 commit comments

Comments
 (0)