oas 20.5.1 → 20.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js +10 -0
- package/dist/lib/helpers.d.ts +1 -0
- package/dist/lib/helpers.js +7 -0
- package/dist/lib/openapi-to-json-schema.d.ts +0 -1
- package/dist/lib/openapi-to-json-schema.js +9 -12
- package/dist/operation/get-parameters-as-json-schema.js +6 -5
- package/dist/operation/get-response-as-json-schema.js +2 -1
- package/package.json +1 -1
- package/src/index.ts +13 -0
- package/src/lib/helpers.ts +3 -0
- package/src/lib/openapi-to-json-schema.ts +3 -5
- package/src/operation/get-parameters-as-json-schema.ts +2 -1
- package/src/operation/get-response-as-json-schema.ts +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## <small>20.5.3 (2023-03-08)</small>
|
|
2
|
+
|
|
3
|
+
* fix: bug where request example selection wouldn't always use the first (#739) ([7c000e0](https://github.com/readmeio/oas/commit/7c000e0)), closes [#739](https://github.com/readmeio/oas/issues/739)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## <small>20.5.2 (2023-03-03)</small>
|
|
8
|
+
|
|
9
|
+
* fix: bug where primitive schemas would crash dereferencing (#738) ([30c06f0](https://github.com/readmeio/oas/commit/30c06f0)), closes [#738](https://github.com/readmeio/oas/issues/738)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
1
13
|
## <small>20.5.1 (2023-03-03)</small>
|
|
2
14
|
|
|
3
15
|
* fix: running prettier to resolve some issues ([bc3c7e4](https://github.com/readmeio/oas/commit/bc3c7e4))
|
package/dist/index.js
CHANGED
|
@@ -78,6 +78,7 @@ var json_schema_ref_parser_1 = __importDefault(require("@readme/json-schema-ref-
|
|
|
78
78
|
var path_to_regexp_1 = require("path-to-regexp");
|
|
79
79
|
var get_auth_1 = __importDefault(require("./lib/get-auth"));
|
|
80
80
|
var get_user_variable_1 = __importDefault(require("./lib/get-user-variable"));
|
|
81
|
+
var helpers_1 = require("./lib/helpers");
|
|
81
82
|
var operation_1 = __importStar(require("./operation"));
|
|
82
83
|
exports.Operation = operation_1["default"];
|
|
83
84
|
exports.Callback = operation_1.Callback;
|
|
@@ -764,6 +765,15 @@ var Oas = /** @class */ (function () {
|
|
|
764
765
|
// what their name is so that when dereferencing happens below those names will be preserved.
|
|
765
766
|
if (api && api.components && api.components.schemas && typeof api.components.schemas === 'object') {
|
|
766
767
|
Object.keys(api.components.schemas).forEach(function (schemaName) {
|
|
768
|
+
// As of OpenAPI 3.1 component schemas can be primitives or arrays. If this happens then we
|
|
769
|
+
// shouldn't try to add `title` or `x-readme-ref-name` properties because we can't. We'll
|
|
770
|
+
// have some data loss on these schemas but as they aren't objects they likely won't be used
|
|
771
|
+
// in ways that would require needing a `title` or `x-readme-ref-name` anyways.
|
|
772
|
+
if ((0, helpers_1.isPrimitive)(api.components.schemas[schemaName]) ||
|
|
773
|
+
Array.isArray(api.components.schemas[schemaName]) ||
|
|
774
|
+
api.components.schemas[schemaName] === null) {
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
767
777
|
if (opts.preserveRefAsJSONSchemaTitle) {
|
|
768
778
|
// This may result in some data loss if there's already a `title` present, but in the case
|
|
769
779
|
// where we want to generate code for the API definition (see http://npm.im/api), we'd
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isPrimitive(val: unknown): boolean;
|
|
@@ -31,7 +31,6 @@ export interface toJSONSchemaOptions {
|
|
|
31
31
|
transformer?: (schema: RMOAS.SchemaObject) => RMOAS.SchemaObject;
|
|
32
32
|
}
|
|
33
33
|
export declare function getSchemaVersionString(schema: RMOAS.SchemaObject, api: RMOAS.OASDocument): string;
|
|
34
|
-
export declare function isPrimitive(val: unknown): boolean;
|
|
35
34
|
/**
|
|
36
35
|
* Given an OpenAPI-flavored JSON Schema, make an effort to modify it so it's shaped more towards
|
|
37
36
|
* stock JSON Schema.
|
|
@@ -57,10 +57,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
57
57
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
58
58
|
};
|
|
59
59
|
exports.__esModule = true;
|
|
60
|
-
exports.
|
|
60
|
+
exports.getSchemaVersionString = void 0;
|
|
61
61
|
var json_schema_merge_allof_1 = __importDefault(require("json-schema-merge-allof"));
|
|
62
62
|
var jsonpointer_1 = __importDefault(require("jsonpointer"));
|
|
63
63
|
var RMOAS = __importStar(require("../rmoas.types"));
|
|
64
|
+
var helpers_1 = require("./helpers");
|
|
64
65
|
/**
|
|
65
66
|
* This list has been pulled from `openapi-schema-to-json-schema` but been slightly modified to fit
|
|
66
67
|
* within the constraints in which ReadMe uses the output from this library in schema form
|
|
@@ -137,10 +138,6 @@ function getSchemaVersionString(schema, api) {
|
|
|
137
138
|
return 'https://json-schema.org/draft/2020-12/schema#';
|
|
138
139
|
}
|
|
139
140
|
exports.getSchemaVersionString = getSchemaVersionString;
|
|
140
|
-
function isPrimitive(val) {
|
|
141
|
-
return typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean';
|
|
142
|
-
}
|
|
143
|
-
exports.isPrimitive = isPrimitive;
|
|
144
141
|
function isPolymorphicSchema(schema) {
|
|
145
142
|
return 'allOf' in schema || 'anyOf' in schema || 'oneOf' in schema;
|
|
146
143
|
}
|
|
@@ -205,7 +202,7 @@ function searchForExampleByPointer(pointer, examples) {
|
|
|
205
202
|
continue;
|
|
206
203
|
}
|
|
207
204
|
// Prevent us from crashing if `examples` is a completely empty object.
|
|
208
|
-
schema = schema.examples.shift();
|
|
205
|
+
schema = __spreadArray([], schema.examples, true).shift();
|
|
209
206
|
}
|
|
210
207
|
try {
|
|
211
208
|
example = jsonpointer_1["default"].get(schema, pointers[i]);
|
|
@@ -370,11 +367,11 @@ function toJSONSchema(data, opts) {
|
|
|
370
367
|
// JSON Schema doesn't support OpenAPI-style examples so we need to reshape them a bit.
|
|
371
368
|
if ('example' in schema) {
|
|
372
369
|
// Only bother adding primitive examples.
|
|
373
|
-
if (isPrimitive(schema.example)) {
|
|
370
|
+
if ((0, helpers_1.isPrimitive)(schema.example)) {
|
|
374
371
|
schema.examples = [schema.example];
|
|
375
372
|
}
|
|
376
373
|
else if (Array.isArray(schema.example)) {
|
|
377
|
-
schema.examples = schema.example.filter(function (example) { return isPrimitive(example); });
|
|
374
|
+
schema.examples = schema.example.filter(function (example) { return (0, helpers_1.isPrimitive)(example); });
|
|
378
375
|
if (!schema.examples.length) {
|
|
379
376
|
delete schema.examples;
|
|
380
377
|
}
|
|
@@ -396,11 +393,11 @@ function toJSONSchema(data, opts) {
|
|
|
396
393
|
refLogger(example.$ref, 'ref');
|
|
397
394
|
}
|
|
398
395
|
else if ('value' in example) {
|
|
399
|
-
if (isPrimitive(example.value)) {
|
|
396
|
+
if ((0, helpers_1.isPrimitive)(example.value)) {
|
|
400
397
|
examples_1.push(example.value);
|
|
401
398
|
reshapedExamples_1 = true;
|
|
402
399
|
}
|
|
403
|
-
else if (Array.isArray(example.value) && isPrimitive(example.value[0])) {
|
|
400
|
+
else if (Array.isArray(example.value) && (0, helpers_1.isPrimitive)(example.value[0])) {
|
|
404
401
|
examples_1.push(example.value[0]);
|
|
405
402
|
reshapedExamples_1 = true;
|
|
406
403
|
}
|
|
@@ -419,7 +416,7 @@ function toJSONSchema(data, opts) {
|
|
|
419
416
|
schema.examples = examples_1;
|
|
420
417
|
}
|
|
421
418
|
}
|
|
422
|
-
else if (Array.isArray(schema.examples) && isPrimitive(schema.examples[0])) {
|
|
419
|
+
else if (Array.isArray(schema.examples) && (0, helpers_1.isPrimitive)(schema.examples[0])) {
|
|
423
420
|
// We haven't reshaped `examples` here, but since it's in a state that's preferrable to us
|
|
424
421
|
// let's keep it around.
|
|
425
422
|
reshapedExamples_1 = true;
|
|
@@ -436,7 +433,7 @@ function toJSONSchema(data, opts) {
|
|
|
436
433
|
if (foundExample) {
|
|
437
434
|
// We can only really deal with primitives, so only promote those as the found example if
|
|
438
435
|
// it is.
|
|
439
|
-
if (isPrimitive(foundExample) || (Array.isArray(foundExample) && isPrimitive(foundExample[0]))) {
|
|
436
|
+
if ((0, helpers_1.isPrimitive)(foundExample) || (Array.isArray(foundExample) && (0, helpers_1.isPrimitive)(foundExample[0]))) {
|
|
440
437
|
schema.examples = [foundExample];
|
|
441
438
|
}
|
|
442
439
|
}
|
|
@@ -39,6 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
exports.__esModule = true;
|
|
40
40
|
exports.types = void 0;
|
|
41
41
|
var clone_object_1 = __importDefault(require("../lib/clone-object"));
|
|
42
|
+
var helpers_1 = require("../lib/helpers");
|
|
42
43
|
var matches_mimetype_1 = __importDefault(require("../lib/matches-mimetype"));
|
|
43
44
|
var openapi_to_json_schema_1 = __importStar(require("../lib/openapi-to-json-schema"));
|
|
44
45
|
var isJSON = matches_mimetype_1["default"].json;
|
|
@@ -115,7 +116,7 @@ function getParametersAsJSONSchema(operation, api, opts) {
|
|
|
115
116
|
});
|
|
116
117
|
return {
|
|
117
118
|
type: type,
|
|
118
|
-
schema: (0,
|
|
119
|
+
schema: (0, helpers_1.isPrimitive)(deprecatedSchema)
|
|
119
120
|
? deprecatedSchema
|
|
120
121
|
: __assign(__assign({}, deprecatedSchema), { $schema: (0, openapi_to_json_schema_1.getSchemaVersionString)(deprecatedSchema, api) })
|
|
121
122
|
};
|
|
@@ -157,7 +158,7 @@ function getParametersAsJSONSchema(operation, api, opts) {
|
|
|
157
158
|
if (!Object.keys(cleanedSchema).length) {
|
|
158
159
|
return null;
|
|
159
160
|
}
|
|
160
|
-
return __assign({ type: type, label: exports.types[type], schema: (0,
|
|
161
|
+
return __assign({ type: type, label: exports.types[type], schema: (0, helpers_1.isPrimitive)(cleanedSchema)
|
|
161
162
|
? cleanedSchema
|
|
162
163
|
: __assign(__assign({}, cleanedSchema), { $schema: (0, openapi_to_json_schema_1.getSchemaVersionString)(cleanedSchema, api) }), deprecatedProps: getDeprecated(cleanedSchema, type) }, (description ? { description: description } : {}));
|
|
163
164
|
}
|
|
@@ -250,7 +251,7 @@ function getParametersAsJSONSchema(operation, api, opts) {
|
|
|
250
251
|
refLogger: refLogger,
|
|
251
252
|
transformer: opts.transformer
|
|
252
253
|
});
|
|
253
|
-
schema = (0,
|
|
254
|
+
schema = (0, helpers_1.isPrimitive)(interimSchema)
|
|
254
255
|
? interimSchema
|
|
255
256
|
: __assign(__assign({}, interimSchema), {
|
|
256
257
|
// Note: this applies a `$schema` version to each field in the larger schema
|
|
@@ -299,7 +300,7 @@ function getParametersAsJSONSchema(operation, api, opts) {
|
|
|
299
300
|
refLogger: refLogger,
|
|
300
301
|
transformer: opts.transformer
|
|
301
302
|
});
|
|
302
|
-
schema = (0,
|
|
303
|
+
schema = (0, helpers_1.isPrimitive)(interimSchema)
|
|
303
304
|
? interimSchema
|
|
304
305
|
: __assign(__assign({}, interimSchema), {
|
|
305
306
|
// Note: this applies a `$schema` version to each field in the larger schema
|
|
@@ -312,7 +313,7 @@ function getParametersAsJSONSchema(operation, api, opts) {
|
|
|
312
313
|
// Parameter descriptions don't exist in `current.schema` so `constructSchema` will never
|
|
313
314
|
// have access to it.
|
|
314
315
|
if (current.description) {
|
|
315
|
-
if (!(0,
|
|
316
|
+
if (!(0, helpers_1.isPrimitive)(schema)) {
|
|
316
317
|
schema.description = current.description;
|
|
317
318
|
}
|
|
318
319
|
}
|
|
@@ -38,6 +38,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
38
38
|
};
|
|
39
39
|
exports.__esModule = true;
|
|
40
40
|
var clone_object_1 = __importDefault(require("../lib/clone-object"));
|
|
41
|
+
var helpers_1 = require("../lib/helpers");
|
|
41
42
|
var matches_mimetype_1 = __importDefault(require("../lib/matches-mimetype"));
|
|
42
43
|
var openapi_to_json_schema_1 = __importStar(require("../lib/openapi-to-json-schema"));
|
|
43
44
|
var isJSON = matches_mimetype_1["default"].json;
|
|
@@ -145,7 +146,7 @@ function getResponseAsJSONSchema(operation, api, statusCode, opts) {
|
|
|
145
146
|
// able to render so instead of generating a JSON Schema with an `undefined` type we should
|
|
146
147
|
// default to `string` so there's at least *something* the end-user can interact with.
|
|
147
148
|
type: foundSchema.type || 'string',
|
|
148
|
-
schema: (0,
|
|
149
|
+
schema: (0, helpers_1.isPrimitive)(schema)
|
|
149
150
|
? schema
|
|
150
151
|
: __assign(__assign({}, schema), { $schema: (0, openapi_to_json_schema_1.getSchemaVersionString)(schema, api) }),
|
|
151
152
|
label: 'Response body'
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { pathToRegexp, match } from 'path-to-regexp';
|
|
|
7
7
|
|
|
8
8
|
import getAuth from './lib/get-auth';
|
|
9
9
|
import getUserVariable from './lib/get-user-variable';
|
|
10
|
+
import { isPrimitive } from './lib/helpers';
|
|
10
11
|
import Operation, { Callback, Webhook } from './operation';
|
|
11
12
|
import utils, { supportedMethods } from './utils';
|
|
12
13
|
|
|
@@ -825,6 +826,18 @@ export default class Oas {
|
|
|
825
826
|
// what their name is so that when dereferencing happens below those names will be preserved.
|
|
826
827
|
if (api && api.components && api.components.schemas && typeof api.components.schemas === 'object') {
|
|
827
828
|
Object.keys(api.components.schemas).forEach(schemaName => {
|
|
829
|
+
// As of OpenAPI 3.1 component schemas can be primitives or arrays. If this happens then we
|
|
830
|
+
// shouldn't try to add `title` or `x-readme-ref-name` properties because we can't. We'll
|
|
831
|
+
// have some data loss on these schemas but as they aren't objects they likely won't be used
|
|
832
|
+
// in ways that would require needing a `title` or `x-readme-ref-name` anyways.
|
|
833
|
+
if (
|
|
834
|
+
isPrimitive(api.components.schemas[schemaName]) ||
|
|
835
|
+
Array.isArray(api.components.schemas[schemaName]) ||
|
|
836
|
+
api.components.schemas[schemaName] === null
|
|
837
|
+
) {
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
|
|
828
841
|
if (opts.preserveRefAsJSONSchemaTitle) {
|
|
829
842
|
// This may result in some data loss if there's already a `title` present, but in the case
|
|
830
843
|
// where we want to generate code for the API definition (see http://npm.im/api), we'd
|
|
@@ -6,6 +6,8 @@ import jsonpointer from 'jsonpointer';
|
|
|
6
6
|
|
|
7
7
|
import * as RMOAS from '../rmoas.types';
|
|
8
8
|
|
|
9
|
+
import { isPrimitive } from './helpers';
|
|
10
|
+
|
|
9
11
|
/**
|
|
10
12
|
* This list has been pulled from `openapi-schema-to-json-schema` but been slightly modified to fit
|
|
11
13
|
* within the constraints in which ReadMe uses the output from this library in schema form
|
|
@@ -133,10 +135,6 @@ export function getSchemaVersionString(schema: RMOAS.SchemaObject, api: RMOAS.OA
|
|
|
133
135
|
return 'https://json-schema.org/draft/2020-12/schema#';
|
|
134
136
|
}
|
|
135
137
|
|
|
136
|
-
export function isPrimitive(val: unknown): boolean {
|
|
137
|
-
return typeof val === 'string' || typeof val === 'number' || typeof val === 'boolean';
|
|
138
|
-
}
|
|
139
|
-
|
|
140
138
|
function isPolymorphicSchema(schema: RMOAS.SchemaObject): boolean {
|
|
141
139
|
return 'allOf' in schema || 'anyOf' in schema || 'oneOf' in schema;
|
|
142
140
|
}
|
|
@@ -206,7 +204,7 @@ function searchForExampleByPointer(pointer: string, examples: PrevSchemasType =
|
|
|
206
204
|
}
|
|
207
205
|
|
|
208
206
|
// Prevent us from crashing if `examples` is a completely empty object.
|
|
209
|
-
schema = schema.examples.shift();
|
|
207
|
+
schema = [...schema.examples].shift();
|
|
210
208
|
}
|
|
211
209
|
|
|
212
210
|
try {
|
|
@@ -3,8 +3,9 @@ import type { ComponentsObject, ExampleObject, OASDocument, ParameterObject, Sch
|
|
|
3
3
|
import type { OpenAPIV3_1 } from 'openapi-types';
|
|
4
4
|
|
|
5
5
|
import cloneObject from '../lib/clone-object';
|
|
6
|
+
import { isPrimitive } from '../lib/helpers';
|
|
6
7
|
import matchesMimetype from '../lib/matches-mimetype';
|
|
7
|
-
import toJSONSchema, {
|
|
8
|
+
import toJSONSchema, { getSchemaVersionString } from '../lib/openapi-to-json-schema';
|
|
8
9
|
|
|
9
10
|
const isJSON = matchesMimetype.json;
|
|
10
11
|
|
|
@@ -9,8 +9,9 @@ import type {
|
|
|
9
9
|
} from 'rmoas.types';
|
|
10
10
|
|
|
11
11
|
import cloneObject from '../lib/clone-object';
|
|
12
|
+
import { isPrimitive } from '../lib/helpers';
|
|
12
13
|
import matches from '../lib/matches-mimetype';
|
|
13
|
-
import toJSONSchema, {
|
|
14
|
+
import toJSONSchema, { getSchemaVersionString } from '../lib/openapi-to-json-schema';
|
|
14
15
|
|
|
15
16
|
const isJSON = matches.json;
|
|
16
17
|
|