@goast/core 0.3.3 → 0.3.5

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.
@@ -11,6 +11,7 @@ function collectOpenApi(apis) {
11
11
  collectDocument(data, apis);
12
12
  return data;
13
13
  }
14
+ const jsonSchemaProperties = ['$id', 'allOf', 'anyOf', 'oneOf', 'enum', 'not', 'properties', 'title', 'type'];
14
15
  function collectDocument(data, documents) {
15
16
  (0, helpers_1.collect)(data, documents, (data, document) => {
16
17
  var _a, _b, _c, _d, _e;
@@ -24,6 +25,9 @@ function collectDocument(data, documents) {
24
25
  (0, helpers_1.collectRecord)(data, (_d = document.components) === null || _d === void 0 ? void 0 : _d.responses, collectResponse);
25
26
  (0, helpers_1.collectRecord)(data, document.responses, collectResponse);
26
27
  (0, helpers_1.collectRecord)(data, (_e = document.components) === null || _e === void 0 ? void 0 : _e.headers, collectHeader);
28
+ if (Object.keys(document).some((key) => jsonSchemaProperties.includes(key))) {
29
+ collectSchema(data, document);
30
+ }
27
31
  });
28
32
  }
29
33
  function collectPaths(data, paths) {
@@ -92,9 +92,9 @@ class OpenApiParser {
92
92
  absoluteRefFile = path_1.default.resolve(path_1.default.dirname(file), refFile);
93
93
  }
94
94
  const doc = await this.loadDocument(absoluteRefFile);
95
- const value = getDeepProperty(doc.document, refPath.replace(/[/]/g, '.'));
95
+ const value = refPath ? getDeepProperty(doc.document, refPath.replace(/[/]/g, '.')) : doc.document;
96
96
  if (value && typeof value === 'object') {
97
- return this.dereference(absoluteRefFile, refPath, value);
97
+ return this.dereference(absoluteRefFile, refPath !== null && refPath !== void 0 ? refPath : '/', value);
98
98
  }
99
99
  return undefined;
100
100
  }
@@ -10,6 +10,7 @@ exports.determineEndpointName = determineEndpointName;
10
10
  exports.transformAdditionalProperties = transformAdditionalProperties;
11
11
  exports.transformSchemaProperties = transformSchemaProperties;
12
12
  exports.getOpenApiObjectIdentifier = getOpenApiObjectIdentifier;
13
+ const path_1 = require("path");
13
14
  const internal_utils_1 = require("../internal-utils");
14
15
  const utils_1 = require("../utils");
15
16
  const common_utils_1 = require("../utils/common.utils");
@@ -18,7 +19,8 @@ function determineSchemaKind(ctx, schema) {
18
19
  return 'oneOf';
19
20
  }
20
21
  else if (schema.type !== 'object' && (schema.allOf || schema.anyOf)) {
21
- return 'combined';
22
+ const hasProperties = (schema.properties && Object.keys(schema.properties).length > 0) || schema.additionalProperties;
23
+ return hasProperties ? 'object' : 'combined';
22
24
  }
23
25
  else if (Array.isArray(schema.type)) {
24
26
  return 'multi-type';
@@ -45,6 +47,8 @@ function determineSchemaName(schema, id) {
45
47
  if (!schema.$src) {
46
48
  console.log('woot?');
47
49
  }
50
+ if (schema.$src.path === '/')
51
+ return { name: (0, path_1.parse)(schema.$src.file).name, isGenerated: true };
48
52
  const schemaNameMatch = schema.$src.path.match(/(?<=\/components\/schemas\/|\/definitions\/)[^/]+$/i);
49
53
  if (schemaNameMatch) {
50
54
  return { name: schemaNameMatch[0], isGenerated: false };
@@ -76,7 +80,15 @@ function determineSchemaName(schema, id) {
76
80
  const parentSchemaMatch = schema.$src.path.match(/(.*)\/properties\/([^/]*)$/);
77
81
  if (parentSchemaMatch) {
78
82
  const parentSchema = (0, utils_1.getDeepProperty)(schema.$src.document, parentSchemaMatch[1].split('/').filter(Boolean));
79
- const parentSchemaName = determineSchemaName({ title: parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title, $src: { path: parentSchemaMatch[1], document: schema.$src.document } }, id);
83
+ const parentSchemaName = determineSchemaName({
84
+ title: parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title,
85
+ $src: {
86
+ file: schema.$src.file,
87
+ path: parentSchemaMatch[1],
88
+ document: schema.$src.document,
89
+ originalComponent: parentSchema,
90
+ },
91
+ }, id);
80
92
  if (!parentSchemaName.isGenerated) {
81
93
  return { name: `${parentSchemaName.name}_${parentSchemaMatch[2]}`, isGenerated: true };
82
94
  }
@@ -79,76 +79,57 @@ function transformSchema(ctx, schema) {
79
79
  ctx.schemas.set(schemaSource, completeSchema);
80
80
  return completeSchema;
81
81
  }
82
- const schemaTransformers = {
83
- oneOf: (schema, context) => {
84
- var _a, _b;
85
- return ({
86
- oneOf: (_b = (_a = schema.oneOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
87
- });
88
- },
89
- string: (schema) => ({
90
- type: 'string',
82
+ function allExtensionsTransformer(schema, context) {
83
+ var _a, _b, _c, _d, _e, _f;
84
+ return {
85
+ oneOf: (_b = (_a = schema.oneOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
91
86
  format: schema.format,
92
87
  pattern: schema.pattern,
93
88
  minLength: schema.minLength,
94
89
  maxLength: schema.maxLength,
95
- }),
96
- number: (schema) => ({
97
- type: 'number',
98
- format: schema.format,
99
90
  minimum: schema.minimum,
100
91
  maximum: schema.maximum,
92
+ properties: (0, helpers_1.transformSchemaProperties)(context, schema, transformSchema),
93
+ additionalProperties: (0, helpers_1.transformAdditionalProperties)(context, schema, transformSchema),
94
+ allOf: (_d = (_c = schema.allOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
95
+ anyOf: (_f = (_e = schema.anyOf) === null || _e === void 0 ? void 0 : _e.map((s) => transformSchema(context, s))) !== null && _f !== void 0 ? _f : [],
96
+ items: schema.items ? transformSchema(context, schema.items) : undefined,
97
+ minItems: schema.minItems,
98
+ maxItems: schema.maxItems,
99
+ };
100
+ }
101
+ const schemaTransformers = {
102
+ oneOf: (schema, context) => ({
103
+ ...allExtensionsTransformer(schema, context),
104
+ }),
105
+ string: (schema, context) => ({
106
+ type: 'string',
107
+ ...allExtensionsTransformer(schema, context),
108
+ }),
109
+ number: (schema, context) => ({
110
+ type: 'number',
111
+ ...allExtensionsTransformer(schema, context),
101
112
  }),
102
- boolean: (schema) => ({ type: 'boolean', format: schema.format }),
103
- object: (schema, context) => {
104
- var _a, _b, _c, _d;
105
- return ({
106
- type: 'object',
107
- properties: (0, helpers_1.transformSchemaProperties)(context, schema, transformSchema),
108
- format: schema.format,
109
- additionalProperties: (0, helpers_1.transformAdditionalProperties)(context, schema, transformSchema),
110
- allOf: (_b = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
111
- anyOf: (_d = (_c = schema.anyOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
112
- });
113
- },
114
- integer: (schema) => ({
113
+ boolean: (schema, context) => ({ type: 'boolean', ...allExtensionsTransformer(schema, context) }),
114
+ object: (schema, context) => ({
115
+ type: 'object',
116
+ ...allExtensionsTransformer(schema, context),
117
+ }),
118
+ integer: (schema, context) => ({
115
119
  type: 'integer',
116
- format: schema.format,
117
- minimum: schema.minimum,
118
- maximum: schema.maximum,
120
+ ...allExtensionsTransformer(schema, context),
119
121
  }),
120
122
  array: (schema, context) => ({
121
123
  type: 'array',
122
- items: schema.items ? transformSchema(context, schema.items) : undefined,
123
- minItems: schema.minItems,
124
- maxItems: schema.maxItems,
124
+ ...allExtensionsTransformer(schema, context),
125
+ }),
126
+ combined: (schema, context) => ({
127
+ ...allExtensionsTransformer(schema, context),
128
+ }),
129
+ 'multi-type': (schema, context) => ({
130
+ type: schema.type,
131
+ ...allExtensionsTransformer(schema, context),
125
132
  }),
126
- combined: (schema, context) => {
127
- var _a, _b, _c, _d;
128
- return ({
129
- allOf: (_b = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
130
- anyOf: (_d = (_c = schema.anyOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
131
- });
132
- },
133
- 'multi-type': (schema, context) => {
134
- var _a, _b, _c, _d;
135
- return ({
136
- type: schema.type,
137
- items: schema.items ? transformSchema(context, schema.items) : undefined,
138
- minItems: schema.minItems,
139
- maxItems: schema.maxItems,
140
- minimum: schema.minimum,
141
- maximum: schema.maximum,
142
- format: schema.format,
143
- properties: (0, helpers_1.transformSchemaProperties)(context, schema, transformSchema),
144
- additionalProperties: (0, helpers_1.transformAdditionalProperties)(context, schema, transformSchema),
145
- allOf: (_b = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
146
- anyOf: (_d = (_c = schema.anyOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
147
- maxLength: schema.maxLength,
148
- minLength: schema.minLength,
149
- pattern: schema.pattern,
150
- });
151
- },
152
133
  null: () => ({ type: 'null' }),
153
134
  unknown: () => ({}),
154
135
  };
@@ -9,6 +9,9 @@ function resolveAnyOfAndAllOf(schema, ignoreNonObjectParts) {
9
9
  }
10
10
  const required = new Set(schema.required);
11
11
  const properties = new Map();
12
+ if ('properties' in schema) {
13
+ schema.properties.forEach((prop) => properties.set(prop.name, prop));
14
+ }
12
15
  collectSubSchemaProperties(schema.allOf, properties, required, false);
13
16
  collectSubSchemaProperties(schema.anyOf, properties, required, true);
14
17
  const initialAdditionalProperties = schema.kind === 'object' ? schema.additionalProperties : undefined;
@@ -8,6 +8,7 @@ export function collectOpenApi(apis) {
8
8
  collectDocument(data, apis);
9
9
  return data;
10
10
  }
11
+ const jsonSchemaProperties = ['$id', 'allOf', 'anyOf', 'oneOf', 'enum', 'not', 'properties', 'title', 'type'];
11
12
  function collectDocument(data, documents) {
12
13
  collect(data, documents, (data, document) => {
13
14
  var _a, _b, _c, _d, _e;
@@ -21,6 +22,9 @@ function collectDocument(data, documents) {
21
22
  collectRecord(data, (_d = document.components) === null || _d === void 0 ? void 0 : _d.responses, collectResponse);
22
23
  collectRecord(data, document.responses, collectResponse);
23
24
  collectRecord(data, (_e = document.components) === null || _e === void 0 ? void 0 : _e.headers, collectHeader);
25
+ if (Object.keys(document).some((key) => jsonSchemaProperties.includes(key))) {
26
+ collectSchema(data, document);
27
+ }
24
28
  });
25
29
  }
26
30
  function collectPaths(data, paths) {
@@ -88,9 +88,9 @@ export class OpenApiParser {
88
88
  absoluteRefFile = path.resolve(path.dirname(file), refFile);
89
89
  }
90
90
  const doc = await this.loadDocument(absoluteRefFile);
91
- const value = getDeepProperty(doc.document, refPath.replace(/[/]/g, '.'));
91
+ const value = refPath ? getDeepProperty(doc.document, refPath.replace(/[/]/g, '.')) : doc.document;
92
92
  if (value && typeof value === 'object') {
93
- return this.dereference(absoluteRefFile, refPath, value);
93
+ return this.dereference(absoluteRefFile, refPath !== null && refPath !== void 0 ? refPath : '/', value);
94
94
  }
95
95
  return undefined;
96
96
  }
@@ -1,3 +1,4 @@
1
+ import { parse as parsePath } from 'path';
1
2
  import { isOpenApiObjectProperty } from '../internal-utils';
2
3
  import { getDeepProperty } from '../utils';
3
4
  import { isNullish } from '../utils/common.utils';
@@ -6,7 +7,8 @@ export function determineSchemaKind(ctx, schema) {
6
7
  return 'oneOf';
7
8
  }
8
9
  else if (schema.type !== 'object' && (schema.allOf || schema.anyOf)) {
9
- return 'combined';
10
+ const hasProperties = (schema.properties && Object.keys(schema.properties).length > 0) || schema.additionalProperties;
11
+ return hasProperties ? 'object' : 'combined';
10
12
  }
11
13
  else if (Array.isArray(schema.type)) {
12
14
  return 'multi-type';
@@ -33,6 +35,8 @@ export function determineSchemaName(schema, id) {
33
35
  if (!schema.$src) {
34
36
  console.log('woot?');
35
37
  }
38
+ if (schema.$src.path === '/')
39
+ return { name: parsePath(schema.$src.file).name, isGenerated: true };
36
40
  const schemaNameMatch = schema.$src.path.match(/(?<=\/components\/schemas\/|\/definitions\/)[^/]+$/i);
37
41
  if (schemaNameMatch) {
38
42
  return { name: schemaNameMatch[0], isGenerated: false };
@@ -64,7 +68,15 @@ export function determineSchemaName(schema, id) {
64
68
  const parentSchemaMatch = schema.$src.path.match(/(.*)\/properties\/([^/]*)$/);
65
69
  if (parentSchemaMatch) {
66
70
  const parentSchema = getDeepProperty(schema.$src.document, parentSchemaMatch[1].split('/').filter(Boolean));
67
- const parentSchemaName = determineSchemaName({ title: parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title, $src: { path: parentSchemaMatch[1], document: schema.$src.document } }, id);
71
+ const parentSchemaName = determineSchemaName({
72
+ title: parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.title,
73
+ $src: {
74
+ file: schema.$src.file,
75
+ path: parentSchemaMatch[1],
76
+ document: schema.$src.document,
77
+ originalComponent: parentSchema,
78
+ },
79
+ }, id);
68
80
  if (!parentSchemaName.isGenerated) {
69
81
  return { name: `${parentSchemaName.name}_${parentSchemaMatch[2]}`, isGenerated: true };
70
82
  }
@@ -76,76 +76,57 @@ export function transformSchema(ctx, schema) {
76
76
  ctx.schemas.set(schemaSource, completeSchema);
77
77
  return completeSchema;
78
78
  }
79
- const schemaTransformers = {
80
- oneOf: (schema, context) => {
81
- var _a, _b;
82
- return ({
83
- oneOf: (_b = (_a = schema.oneOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
84
- });
85
- },
86
- string: (schema) => ({
87
- type: 'string',
79
+ function allExtensionsTransformer(schema, context) {
80
+ var _a, _b, _c, _d, _e, _f;
81
+ return {
82
+ oneOf: (_b = (_a = schema.oneOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
88
83
  format: schema.format,
89
84
  pattern: schema.pattern,
90
85
  minLength: schema.minLength,
91
86
  maxLength: schema.maxLength,
92
- }),
93
- number: (schema) => ({
94
- type: 'number',
95
- format: schema.format,
96
87
  minimum: schema.minimum,
97
88
  maximum: schema.maximum,
89
+ properties: transformSchemaProperties(context, schema, transformSchema),
90
+ additionalProperties: transformAdditionalProperties(context, schema, transformSchema),
91
+ allOf: (_d = (_c = schema.allOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
92
+ anyOf: (_f = (_e = schema.anyOf) === null || _e === void 0 ? void 0 : _e.map((s) => transformSchema(context, s))) !== null && _f !== void 0 ? _f : [],
93
+ items: schema.items ? transformSchema(context, schema.items) : undefined,
94
+ minItems: schema.minItems,
95
+ maxItems: schema.maxItems,
96
+ };
97
+ }
98
+ const schemaTransformers = {
99
+ oneOf: (schema, context) => ({
100
+ ...allExtensionsTransformer(schema, context),
101
+ }),
102
+ string: (schema, context) => ({
103
+ type: 'string',
104
+ ...allExtensionsTransformer(schema, context),
105
+ }),
106
+ number: (schema, context) => ({
107
+ type: 'number',
108
+ ...allExtensionsTransformer(schema, context),
98
109
  }),
99
- boolean: (schema) => ({ type: 'boolean', format: schema.format }),
100
- object: (schema, context) => {
101
- var _a, _b, _c, _d;
102
- return ({
103
- type: 'object',
104
- properties: transformSchemaProperties(context, schema, transformSchema),
105
- format: schema.format,
106
- additionalProperties: transformAdditionalProperties(context, schema, transformSchema),
107
- allOf: (_b = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
108
- anyOf: (_d = (_c = schema.anyOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
109
- });
110
- },
111
- integer: (schema) => ({
110
+ boolean: (schema, context) => ({ type: 'boolean', ...allExtensionsTransformer(schema, context) }),
111
+ object: (schema, context) => ({
112
+ type: 'object',
113
+ ...allExtensionsTransformer(schema, context),
114
+ }),
115
+ integer: (schema, context) => ({
112
116
  type: 'integer',
113
- format: schema.format,
114
- minimum: schema.minimum,
115
- maximum: schema.maximum,
117
+ ...allExtensionsTransformer(schema, context),
116
118
  }),
117
119
  array: (schema, context) => ({
118
120
  type: 'array',
119
- items: schema.items ? transformSchema(context, schema.items) : undefined,
120
- minItems: schema.minItems,
121
- maxItems: schema.maxItems,
121
+ ...allExtensionsTransformer(schema, context),
122
+ }),
123
+ combined: (schema, context) => ({
124
+ ...allExtensionsTransformer(schema, context),
125
+ }),
126
+ 'multi-type': (schema, context) => ({
127
+ type: schema.type,
128
+ ...allExtensionsTransformer(schema, context),
122
129
  }),
123
- combined: (schema, context) => {
124
- var _a, _b, _c, _d;
125
- return ({
126
- allOf: (_b = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
127
- anyOf: (_d = (_c = schema.anyOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
128
- });
129
- },
130
- 'multi-type': (schema, context) => {
131
- var _a, _b, _c, _d;
132
- return ({
133
- type: schema.type,
134
- items: schema.items ? transformSchema(context, schema.items) : undefined,
135
- minItems: schema.minItems,
136
- maxItems: schema.maxItems,
137
- minimum: schema.minimum,
138
- maximum: schema.maximum,
139
- format: schema.format,
140
- properties: transformSchemaProperties(context, schema, transformSchema),
141
- additionalProperties: transformAdditionalProperties(context, schema, transformSchema),
142
- allOf: (_b = (_a = schema.allOf) === null || _a === void 0 ? void 0 : _a.map((s) => transformSchema(context, s))) !== null && _b !== void 0 ? _b : [],
143
- anyOf: (_d = (_c = schema.anyOf) === null || _c === void 0 ? void 0 : _c.map((s) => transformSchema(context, s))) !== null && _d !== void 0 ? _d : [],
144
- maxLength: schema.maxLength,
145
- minLength: schema.minLength,
146
- pattern: schema.pattern,
147
- });
148
- },
149
130
  null: () => ({ type: 'null' }),
150
131
  unknown: () => ({}),
151
132
  };
@@ -5,6 +5,9 @@ export function resolveAnyOfAndAllOf(schema, ignoreNonObjectParts) {
5
5
  }
6
6
  const required = new Set(schema.required);
7
7
  const properties = new Map();
8
+ if ('properties' in schema) {
9
+ schema.properties.forEach((prop) => properties.set(prop.name, prop));
10
+ }
8
11
  collectSubSchemaProperties(schema.allOf, properties, required, false);
9
12
  collectSubSchemaProperties(schema.anyOf, properties, required, true);
10
13
  const initialAdditionalProperties = schema.kind === 'object' ? schema.additionalProperties : undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/core",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "description": "A tool designed to transform OpenAPI specifications into various forms with flexibility and extensibility at its core.",
6
6
  "keywords": [
@@ -1,19 +1,17 @@
1
1
  import { ApiSchemaKind, ApiSchemaAccessibility, ApiSchema, ApiSchemaProperty } from './api-types';
2
2
  import { OpenApiTransformerContext } from './types';
3
- import { Deref, OpenApiDocument, OpenApiObject } from '../parse';
3
+ import { Deref, DerefSource, OpenApiObject } from '../parse';
4
4
  export declare function determineSchemaKind<T extends {
5
5
  oneOf?: unknown;
6
6
  allOf?: unknown;
7
7
  anyOf?: unknown;
8
8
  type?: string | string[];
9
9
  properties?: Record<string, unknown>;
10
+ additionalProperties?: unknown;
10
11
  }>(ctx: OpenApiTransformerContext, schema: T): ApiSchemaKind;
11
12
  export declare function determineSchemaName(schema: {
12
13
  title?: string;
13
- $src: {
14
- path: string;
15
- document: Deref<OpenApiDocument>;
16
- };
14
+ $src: DerefSource<any>;
17
15
  }, id: string): {
18
16
  name: string;
19
17
  isGenerated: boolean;