Skip to content

Commit df9aadf

Browse files
committed
feat(oas31): enable overrides for samples plugin (#8731)
These overrides are specific to OpenAPI 3.1.0 and JSON Schema 2020-12. Refs #8577
1 parent 2ce9d08 commit df9aadf

File tree

6 files changed

+90
-73
lines changed

6 files changed

+90
-73
lines changed

src/core/plugins/json-schema-2020-12/samples-extensions/fn.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import XML from "xml"
55
import RandExp from "randexp"
66
import isEmpty from "lodash/isEmpty"
7+
78
import { objectify, isFunc, normalizeArray, deeplyStripKey } from "core/utils"
89
import memoizeN from "../../../../helpers/memoizeN"
910

1011
const generateStringFromRegex = (pattern) => {
1112
try {
1213
const randexp = new RandExp(pattern)
1314
return randexp.gen()
14-
} catch (e) {
15-
// Invalid regex should not cause a crash (regex syntax varies across languages)
15+
} catch {
16+
// invalid regex should not cause a crash (regex syntax varies across languages)
1617
return "string"
1718
}
1819
}
@@ -36,17 +37,16 @@ const primitives = {
3637

3738
const primitive = (schema) => {
3839
schema = objectify(schema)
39-
let { type, format } = schema
40-
41-
let fn = primitives[`${type}_${format}`] || primitives[type]
42-
43-
if (isFunc(fn)) return fn(schema)
40+
const { type, format } = schema
41+
const fn = primitives[`${type}_${format}`] || primitives[type]
4442

45-
return "Unknown Type: " + schema.type
43+
return typeof fn === "function" ? fn(schema) : `Unknown Type: ${schema.type}`
4644
}
4745

48-
// do a couple of quick sanity tests to ensure the value
49-
// looks like a $$ref that swagger-client generates.
46+
/**
47+
* Do a couple of quick sanity tests to ensure the value
48+
* looks like a $$ref that swagger-client generates.
49+
*/
5050
const sanitizeRef = (value) =>
5151
deeplyStripKey(
5252
value,
@@ -720,8 +720,9 @@ export const createXMLExample = (schema, config, o) => {
720720
return XML(json, { declaration: true, indent: "\t" })
721721
}
722722

723-
export const sampleFromSchema = (schema, config, o) =>
724-
sampleFromSchemaGeneric(schema, config, o, false)
723+
export const sampleFromSchema = (schema, config, o) => {
724+
return sampleFromSchemaGeneric(schema, config, o, false)
725+
}
725726

726727
const resolver = (arg1, arg2, arg3) => [
727728
arg1,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @prettier
3+
*/
4+
import {
5+
makeIsExpandable,
6+
getProperties,
7+
} from "./json-schema-2020-12-extensions/fn"
8+
import { wrapOAS31Fn } from "./fn"
9+
10+
function afterLoad({ fn, getSystem }) {
11+
// overrides for fn.jsonSchema202012
12+
if (fn.jsonSchema202012) {
13+
const isExpandable = makeIsExpandable(
14+
fn.jsonSchema202012.isExpandable,
15+
getSystem
16+
)
17+
18+
Object.assign(this.fn.jsonSchema202012, { isExpandable, getProperties })
19+
}
20+
21+
// wraps schema generators from samples plugin and make them specific to OpenAPI 3.1 version
22+
if (typeof fn.sampleFromSchema === "function" && fn.jsonSchema202012) {
23+
const wrappedFns = wrapOAS31Fn(
24+
{
25+
sampleFromSchema: fn.jsonSchema202012.sampleFromSchema,
26+
sampleFromSchemaGeneric: fn.jsonSchema202012.sampleFromSchemaGeneric,
27+
createXMLExample: fn.jsonSchema202012.createXMLExample,
28+
memoizedSampleFromSchema: fn.jsonSchema202012.memoizedSampleFromSchema,
29+
memoizedCreateXMLExample: fn.jsonSchema202012.memoizedCreateXMLExample,
30+
},
31+
getSystem()
32+
)
33+
34+
Object.assign(this.fn, wrappedFns)
35+
}
36+
}
37+
38+
export default afterLoad

src/core/plugins/oas31/fn.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,28 @@ export const createOnlyOAS31ComponentWrapper =
9797
}
9898
/* eslint-enable react/jsx-filename-extension */
9999

100-
/** Utilize JSON Schema 2020-12 samples **/
101-
const wrapSampleFn =
102-
(fnName) =>
103-
(getSystem) =>
104-
(...args) => {
105-
const { fn, specSelectors } = getSystem()
106-
107-
if (specSelectors.isOpenAPI31()) {
108-
return fn.jsonSchema202012[fnName](...args)
109-
}
100+
/**
101+
* Runs the fn replacement implementation when spec is OpenAPI 3.1.
102+
* Runs the fn original implementation otherwise.
103+
*
104+
* @param fn
105+
* @param system
106+
* @returns {{[p: string]: function(...[*]): *}}
107+
*/
108+
export const wrapOAS31Fn = (fn, system) => {
109+
const { fn: systemFn, specSelectors } = system
110110

111-
return fn[fnName](...args)
112-
}
111+
return Object.fromEntries(
112+
Object.entries(fn).map(([name, newImpl]) => {
113+
const oriImpl = systemFn[name]
114+
const impl = (...args) =>
115+
specSelectors.isOAS31()
116+
? newImpl(...args)
117+
: typeof oriImpl === "function"
118+
? oriImpl(...args)
119+
: undefined
113120

114-
export const wrapSampleFromSchema = wrapSampleFn("sampleFromSchema")
115-
export const wrapSampleFromSchemaGeneric = wrapSampleFn(
116-
"sampleFromSchemaGeneric"
117-
)
118-
export const wrapCreateXMLExample = wrapSampleFn("createXMLExample")
119-
export const wrapMemoizedSampleFromSchema = wrapSampleFn(
120-
"memoizedSampleFromSchema"
121-
)
122-
export const wrapMemoizedCreateXMLExample = wrapSampleFn(
123-
"memoizedCreateXMLExample"
124-
)
121+
return [name, impl]
122+
})
123+
)
124+
}

src/core/plugins/oas31/index.js

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ import {
2020
isOAS31 as isOAS31Fn,
2121
createOnlyOAS31Selector as createOnlyOAS31SelectorFn,
2222
createSystemSelector as createSystemSelectorFn,
23-
wrapSampleFromSchema,
24-
wrapSampleFromSchemaGeneric,
25-
wrapCreateXMLExample,
26-
wrapMemoizedSampleFromSchema,
27-
wrapMemoizedCreateXMLExample,
2823
} from "./fn"
2924
import {
3025
license as selectLicense,
@@ -64,40 +59,19 @@ import JSONSchema202012KeywordExternalDocs from "./json-schema-2020-12-extension
6459
import JSONSchema202012KeywordDescriptionWrapper from "./json-schema-2020-12-extensions/wrap-components/keywords/Description"
6560
import JSONSchema202012KeywordDefaultWrapper from "./json-schema-2020-12-extensions/wrap-components/keywords/Default"
6661
import JSONSchema202012KeywordPropertiesWrapper from "./json-schema-2020-12-extensions/wrap-components/keywords/Properties"
67-
import {
68-
makeIsExpandable,
69-
getProperties,
70-
} from "./json-schema-2020-12-extensions/fn"
62+
import afterLoad from "./after-load"
7163

72-
const OAS31Plugin = ({ getSystem }) => {
73-
const system = getSystem()
74-
const { fn } = system
64+
const OAS31Plugin = ({ fn }) => {
7565
const createSystemSelector = fn.createSystemSelector || createSystemSelectorFn
7666
const createOnlyOAS31Selector = fn.createOnlyOAS31Selector || createOnlyOAS31SelectorFn // prettier-ignore
7767

78-
const pluginFn = {
79-
isOAs31: isOAS31Fn,
80-
createSystemSelector: createSystemSelectorFn,
81-
createOnlyOAS31Selector: createOnlyOAS31SelectorFn,
82-
}
83-
if (typeof fn.jsonSchema202012?.isExpandable === "function") {
84-
pluginFn.jsonSchema202012 = {
85-
...fn.jsonSchema202012,
86-
isExpandable: makeIsExpandable(fn.jsonSchema202012.isExpandable, system),
87-
getProperties,
88-
}
89-
}
90-
// wraps schema generators and make them specific to OpenAPI version
91-
if (typeof system.fn.inferSchema === "function") {
92-
pluginFn.sampleFromSchema = wrapSampleFromSchema(getSystem)
93-
pluginFn.sampleFromSchemaGeneric = wrapSampleFromSchemaGeneric(getSystem)
94-
pluginFn.createXMLExample = wrapCreateXMLExample(getSystem)
95-
pluginFn.memoizedSampleFromSchema = wrapMemoizedSampleFromSchema(getSystem)
96-
pluginFn.memoizedCreateXMLExample = wrapMemoizedCreateXMLExample(getSystem)
97-
}
98-
9968
return {
100-
fn: pluginFn,
69+
afterLoad,
70+
fn: {
71+
isOAS31: isOAS31Fn,
72+
createSystemSelector: createSystemSelectorFn,
73+
createOnlyOAS31Selector: createOnlyOAS31SelectorFn,
74+
},
10175
components: {
10276
Webhooks,
10377
JsonSchemaDialect,

src/core/plugins/oas31/json-schema-2020-12-extensions/fn.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/**
22
* @prettier
33
*/
4-
export const makeIsExpandable = (original, { fn }) => {
4+
export const makeIsExpandable = (original, getSystem) => {
5+
const { fn } = getSystem()
6+
57
if (typeof original !== "function") {
68
return null
79
}

src/core/plugins/oas31/wrap-components/models.jsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
} from "../json-schema-2020-12-extensions/fn"
1111

1212
const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
13-
const system = getSystem()
14-
const { getComponent, fn, getConfigs } = system
13+
const { getComponent, fn, getConfigs } = getSystem()
1514
const configs = getConfigs()
1615

1716
if (ModelsWrapper.ModelsWithJSONSchemaContext) {
@@ -135,7 +134,10 @@ const ModelsWrapper = createOnlyOAS31ComponentWrapper(({ getSystem }) => {
135134
},
136135
fn: {
137136
upperFirst: fn.upperFirst,
138-
isExpandable: makeIsExpandable(fn.jsonSchema202012.isExpandable, system),
137+
isExpandable: makeIsExpandable(
138+
fn.jsonSchema202012.isExpandable,
139+
getSystem
140+
),
139141
getProperties,
140142
},
141143
})

0 commit comments

Comments
 (0)