Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 5f30229

Browse files
committed
refactor(oas3): make schema component parsing standard behaviour
1 parent 47223e4 commit 5f30229

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

packages/openapi3-parser/lib/parser/oas/parseComponentsObject.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ const parseComponentMember = R.curry((context, parser, member) => {
7171
return parseResult;
7272
});
7373

74+
function registerComponentStateInContext(context, components) {
75+
const { namespace } = context;
76+
77+
// Component referencing supports recursive (and circular in some cases)
78+
// references and thus we must know about all of the component IDs upfront.
79+
// Below we are putting in the unparsed components so we can keep the
80+
// dereferencing logic simple, these are used during parsing the components
81+
// and later on the components in our context is replaced by the final parsed
82+
// result.
83+
// eslint-disable-next-line no-param-reassign
84+
context.state.components = new namespace.elements.Object();
85+
86+
if (isObject(components)) {
87+
components.forEach((value, key) => {
88+
if (isObject(value)) {
89+
// Take each component object (f.e schemas, parameters) and convert to
90+
// object with members for each key (discarding value). We don't want the
91+
// value making it into final parse results under any circumstance, for
92+
// example if the parse errors out and we leave bad state
93+
94+
const componentObject = new namespace.elements.Object(
95+
value.map((value, key) => new namespace.elements.Member(key))
96+
);
97+
98+
context.state.components.set(key.toValue(), componentObject);
99+
}
100+
});
101+
}
102+
}
103+
74104
/**
75105
* Parse Components Object
76106
*
@@ -84,24 +114,7 @@ const parseComponentMember = R.curry((context, parser, member) => {
84114
function parseComponentsObject(context, element) {
85115
const { namespace } = context;
86116

87-
// Schema Object supports recursive (and circular) references and thus we
88-
// must know about all of the schema IDs upfront. Below we are putting
89-
// in the unparsed schemas so we can keep the dereferencing logic simple,
90-
// these are used during parsing the schema components and later on the
91-
// components in our context is replaced by the final parsed result.
92-
// eslint-disable-next-line no-param-reassign
93-
context.state.components = new namespace.elements.Object();
94-
95-
if (isObject(element) && element.get('schemas') && isObject(element.get('schemas'))) {
96-
// Take schemas and convert to object with members for each key (discarding value)
97-
// We don't want the value making it into final parse results under any circumstance,
98-
// for example if the parse errors out and we leave bad state
99-
const schemas = new namespace.elements.Object(
100-
element.get('schemas').map((value, key) => new namespace.elements.Member(key))
101-
);
102-
103-
context.state.components.set('schemas', schemas);
104-
}
117+
registerComponentStateInContext(context, element);
105118

106119
const createMemberValueNotObjectWarning = member => createWarning(namespace,
107120
`'${name}' '${member.key.toValue()}' is not an object`, member.value);
@@ -128,8 +141,6 @@ function parseComponentsObject(context, element) {
128141

129142
if (contextMember) {
130143
contextMember.value = object;
131-
} else {
132-
context.state.components.push(new namespace.elements.Member(member.key, object));
133144
}
134145

135146
return object;

0 commit comments

Comments
 (0)