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

Normalise OpenAPI type as an array internally #598

Merged
merged 1 commit into from
Feb 22, 2021
Merged

Conversation

kylef
Copy link
Member

@kylef kylef commented Feb 22, 2021

This is one of the first steps in supporting OpenAPI 3.1's type as an array. This first step normalises the type into an array internally, so that all the internal code paths in the code base use arrays.

This means that the parseType function in the schema parser will always return an array of strings. In this implementation that array will always be of a single item. There is code which would throw if that wasn't the case. It is not possible to enter the code path, this will be altered in the future to something along the lines of:

@@ -271,7 +270,7 @@ function parseSchema(context) {
       } else if (enumerations) {
         element = enumerations;
       } else if (type.length > 1) {
-        throw new Error('Implementation error: unexpected multiple types');
+        element = type.map(type => constructStructure(namespace, schema, type));
       } else if (type.length === 1) {
         element = constructStructure(namespace, schema, type[0]);
       } else {

but first, the parser needs to be able to parse the arrays, and there will be various tests for all those cases. That will come later.

One thing I will point out, is that parseString accepts a member element and not a string, that's because it uses the member element's key to construct the error message, this makes it hard to compose with other subparsers such as parseArray which is why the string parsing is done inline. parseType accepts a string element (and in the future an array of string elements if you use OpenAPI 3.1). In the past, parseType accepted the member element with the value of a string element.

@kylef kylef added the openapi3 label Feb 22, 2021
@kylef kylef requested a review from opichals February 22, 2021 18:19
);

return pipeParseResult(context.namespace,
parseString(context, name, false),
ensureValidType);
R.unless(isString, value => createWarning(context.namespace, `'${name}' 'type' is not a string`, value)),
Copy link
Member Author

Choose a reason for hiding this comment

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

Eventually this branch will head into a direction where we handle multiple types, I'd imagine it will use R.cond such as:

R.cond([
  [isString, ensureValidType, then cocerce to array],
  [isArray, ...],
  [R.T, createWarning(value must be ...)],
])

Copy link
Contributor

@opichals opichals left a comment

Choose a reason for hiding this comment

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

👌

@kylef kylef merged commit a76c680 into master Feb 22, 2021
@kylef kylef deleted the kylef/type-array branch February 22, 2021 19:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants