@scalar/json-magic 0.8.2 → 0.8.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.
Files changed (84) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/bundle/index.d.ts +1 -0
  3. package/dist/bundle/index.d.ts.map +1 -1
  4. package/dist/bundle/index.js.map +1 -1
  5. package/dist/bundle/plugins/browser.js.map +1 -1
  6. package/dist/bundle/plugins/node.d.ts +1 -1
  7. package/dist/bundle/plugins/node.js +1 -1
  8. package/dist/bundle/plugins/node.js.map +1 -1
  9. package/dist/dereference/index.d.ts.map +1 -1
  10. package/dist/dereference/index.js.map +2 -2
  11. package/dist/diff/index.d.ts +1 -1
  12. package/dist/diff/index.d.ts.map +1 -1
  13. package/dist/diff/index.js +1 -1
  14. package/dist/diff/index.js.map +2 -2
  15. package/dist/helpers/escape-json-pointer.d.ts +1 -1
  16. package/dist/helpers/escape-json-pointer.js.map +1 -1
  17. package/dist/magic-proxy/index.d.ts.map +1 -1
  18. package/dist/magic-proxy/index.js.map +2 -2
  19. package/dist/magic-proxy/proxy.d.ts +0 -1
  20. package/dist/magic-proxy/proxy.d.ts.map +1 -1
  21. package/dist/magic-proxy/proxy.js +1 -2
  22. package/dist/magic-proxy/proxy.js.map +2 -2
  23. package/package.json +12 -13
  24. package/.turbo/turbo-build.log +0 -10
  25. package/esbuild.ts +0 -15
  26. package/src/bundle/bundle.test.ts +0 -2917
  27. package/src/bundle/bundle.ts +0 -916
  28. package/src/bundle/create-limiter.test.ts +0 -28
  29. package/src/bundle/create-limiter.ts +0 -52
  30. package/src/bundle/index.ts +0 -3
  31. package/src/bundle/plugins/browser.ts +0 -4
  32. package/src/bundle/plugins/fetch-urls/index.test.ts +0 -141
  33. package/src/bundle/plugins/fetch-urls/index.ts +0 -105
  34. package/src/bundle/plugins/node.ts +0 -5
  35. package/src/bundle/plugins/parse-json/index.test.ts +0 -24
  36. package/src/bundle/plugins/parse-json/index.ts +0 -32
  37. package/src/bundle/plugins/parse-yaml/index.test.ts +0 -26
  38. package/src/bundle/plugins/parse-yaml/index.ts +0 -34
  39. package/src/bundle/plugins/read-files/index.test.ts +0 -36
  40. package/src/bundle/plugins/read-files/index.ts +0 -58
  41. package/src/bundle/value-generator.test.ts +0 -165
  42. package/src/bundle/value-generator.ts +0 -143
  43. package/src/dereference/dereference.test.ts +0 -142
  44. package/src/dereference/dereference.ts +0 -84
  45. package/src/dereference/index.ts +0 -2
  46. package/src/diff/apply.test.ts +0 -262
  47. package/src/diff/apply.ts +0 -83
  48. package/src/diff/diff.test.ts +0 -328
  49. package/src/diff/diff.ts +0 -93
  50. package/src/diff/index.test.ts +0 -150
  51. package/src/diff/index.ts +0 -5
  52. package/src/diff/merge.test.ts +0 -1109
  53. package/src/diff/merge.ts +0 -136
  54. package/src/diff/trie.test.ts +0 -30
  55. package/src/diff/trie.ts +0 -113
  56. package/src/diff/utils.test.ts +0 -169
  57. package/src/diff/utils.ts +0 -111
  58. package/src/helpers/convert-to-local-ref.test.ts +0 -211
  59. package/src/helpers/convert-to-local-ref.ts +0 -43
  60. package/src/helpers/escape-json-pointer.test.ts +0 -13
  61. package/src/helpers/escape-json-pointer.ts +0 -8
  62. package/src/helpers/get-schemas.test.ts +0 -356
  63. package/src/helpers/get-schemas.ts +0 -80
  64. package/src/helpers/get-segments-from-path.test.ts +0 -17
  65. package/src/helpers/get-segments-from-path.ts +0 -17
  66. package/src/helpers/get-value-by-path.test.ts +0 -338
  67. package/src/helpers/get-value-by-path.ts +0 -44
  68. package/src/helpers/is-json-object.ts +0 -31
  69. package/src/helpers/is-object.test.ts +0 -27
  70. package/src/helpers/is-object.ts +0 -4
  71. package/src/helpers/is-yaml.ts +0 -18
  72. package/src/helpers/json-path-utils.test.ts +0 -57
  73. package/src/helpers/json-path-utils.ts +0 -50
  74. package/src/helpers/normalize.test.ts +0 -92
  75. package/src/helpers/normalize.ts +0 -35
  76. package/src/helpers/unescape-json-pointer.test.ts +0 -23
  77. package/src/helpers/unescape-json-pointer.ts +0 -9
  78. package/src/magic-proxy/index.ts +0 -2
  79. package/src/magic-proxy/proxy.test.ts +0 -1987
  80. package/src/magic-proxy/proxy.ts +0 -323
  81. package/src/types.ts +0 -1
  82. package/tsconfig.build.json +0 -12
  83. package/tsconfig.json +0 -16
  84. package/vite.config.ts +0 -8
@@ -1,211 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { convertToLocalRef } from './convert-to-local-ref'
4
-
5
- describe('convertToLocalRef', () => {
6
- const schemas = new Map([
7
- ['https://example.com/schema1.json', '/components/schemas/Schema1'],
8
- ['https://example.com/schema2.json', '/components/schemas/Schema2'],
9
- ['https://example.com/schema1.json#anchor1', '/components/schemas/Schema1/definitions/Anchor1'],
10
- ['https://example.com/schema2.json#anchor2', '/components/schemas/Schema2/definitions/Anchor2'],
11
- ['https://example.com/current.json#currentAnchor', '/components/schemas/Current/definitions/CurrentAnchor'],
12
- ])
13
-
14
- const currentContext = 'https://example.com/current.json'
15
-
16
- describe('external references with baseUrl', () => {
17
- it('should resolve external reference without path or anchor', () => {
18
- const result = convertToLocalRef('https://example.com/schema1.json', currentContext, schemas)
19
-
20
- expect(result).toBe('/components/schemas/Schema1')
21
- })
22
-
23
- it('should resolve external reference with JSON pointer path', () => {
24
- const result = convertToLocalRef('https://example.com/schema1.json#/definitions/User', currentContext, schemas)
25
-
26
- expect(result).toBe('/components/schemas/Schema1/definitions/User')
27
- })
28
-
29
- it('should resolve external reference with anchor', () => {
30
- const result = convertToLocalRef('https://example.com/schema1.json#anchor1', currentContext, schemas)
31
-
32
- expect(result).toBe('/components/schemas/Schema1/definitions/Anchor1')
33
- })
34
-
35
- it('should return undefined for external reference not in schemas', () => {
36
- const result = convertToLocalRef('https://example.com/unknown.json', currentContext, schemas)
37
-
38
- expect(result).toBeUndefined()
39
- })
40
-
41
- it('should return undefined for external reference with unknown anchor', () => {
42
- const result = convertToLocalRef('https://example.com/schema1.json#unknownAnchor', currentContext, schemas)
43
-
44
- expect(result).toBeUndefined()
45
- })
46
- })
47
-
48
- describe('local references without baseUrl', () => {
49
- it('should resolve local JSON pointer path', () => {
50
- const result = convertToLocalRef('#/definitions/User', currentContext, schemas)
51
-
52
- expect(result).toBe('definitions/User')
53
- })
54
-
55
- it('should resolve local JSON pointer path with multiple segments', () => {
56
- const result = convertToLocalRef('#/components/schemas/User/properties/name', currentContext, schemas)
57
-
58
- expect(result).toBe('components/schemas/User/properties/name')
59
- })
60
-
61
- it('should resolve local anchor reference', () => {
62
- const result = convertToLocalRef('#currentAnchor', currentContext, schemas)
63
-
64
- expect(result).toBe('/components/schemas/Current/definitions/CurrentAnchor')
65
- })
66
-
67
- it('should return undefined for local anchor not in schemas', () => {
68
- const result = convertToLocalRef('#unknownAnchor', currentContext, schemas)
69
-
70
- expect(result).toBeUndefined()
71
- })
72
- })
73
-
74
- describe('edge cases', () => {
75
- it('should return undefined for empty reference', () => {
76
- const result = convertToLocalRef('', currentContext, schemas)
77
-
78
- expect(result).toBeUndefined()
79
- })
80
-
81
- it('should return undefined for reference with only hash', () => {
82
- const result = convertToLocalRef('#', currentContext, schemas)
83
-
84
- expect(result).toBeUndefined()
85
- })
86
-
87
- it('should return undefined for reference with only baseUrl and hash', () => {
88
- const result = convertToLocalRef('https://example.com/schema1.json#', currentContext, schemas)
89
-
90
- expect(result).toBe('/components/schemas/Schema1')
91
- })
92
-
93
- it('should handle empty currentContext', () => {
94
- const result = convertToLocalRef('#anchor', '', schemas)
95
-
96
- expect(result).toBeUndefined()
97
- })
98
-
99
- it('should handle empty schemas map', () => {
100
- const emptySchemas = new Map()
101
- const result = convertToLocalRef('https://example.com/schema1.json', currentContext, emptySchemas)
102
-
103
- expect(result).toBeUndefined()
104
- })
105
- })
106
-
107
- describe('complex scenarios', () => {
108
- it('should handle nested JSON pointer paths', () => {
109
- const result = convertToLocalRef(
110
- 'https://example.com/schema1.json#/definitions/User/properties/address/properties/street',
111
- currentContext,
112
- schemas,
113
- )
114
-
115
- expect(result).toBe('/components/schemas/Schema1/definitions/User/properties/address/properties/street')
116
- })
117
-
118
- it('should handle anchor with special characters', () => {
119
- const schemasWithSpecialChars = new Map([
120
- ['https://example.com/schema1.json', '/components/schemas/Schema1'],
121
- [
122
- 'https://example.com/schema1.json#anchor-with-dashes',
123
- '/components/schemas/Schema1/definitions/AnchorWithDashes',
124
- ],
125
- [
126
- 'https://example.com/schema1.json#anchor_with_underscores',
127
- '/components/schemas/Schema1/definitions/AnchorWithUnderscores',
128
- ],
129
- ])
130
-
131
- const result1 = convertToLocalRef(
132
- 'https://example.com/schema1.json#anchor-with-dashes',
133
- currentContext,
134
- schemasWithSpecialChars,
135
- )
136
-
137
- const result2 = convertToLocalRef(
138
- 'https://example.com/schema1.json#anchor_with_underscores',
139
- currentContext,
140
- schemasWithSpecialChars,
141
- )
142
-
143
- expect(result1).toBe('/components/schemas/Schema1/definitions/AnchorWithDashes')
144
- expect(result2).toBe('/components/schemas/Schema1/definitions/AnchorWithUnderscores')
145
- })
146
-
147
- it('should handle different schema contexts', () => {
148
- const differentContext = 'https://example.com/different.json'
149
- const schemasWithDifferentContext = new Map([
150
- [
151
- 'https://example.com/different.json#differentAnchor',
152
- '/components/schemas/Different/definitions/DifferentAnchor',
153
- ],
154
- ])
155
-
156
- const result = convertToLocalRef('#differentAnchor', differentContext, schemasWithDifferentContext)
157
-
158
- expect(result).toBe('/components/schemas/Different/definitions/DifferentAnchor')
159
- })
160
- })
161
-
162
- describe('real-world examples', () => {
163
- it('should handle OpenAPI component references', () => {
164
- const openApiSchemas = new Map([
165
- ['https://api.example.com/schemas/user.json', '/components/schemas/User'],
166
- ['https://api.example.com/schemas/user.json#/properties/id', '/components/schemas/User/properties/id'],
167
- ['https://api.example.com/schemas/user.json#userProfile', '/components/schemas/User/definitions/UserProfile'],
168
- ])
169
-
170
- const result1 = convertToLocalRef(
171
- 'https://api.example.com/schemas/user.json',
172
- 'https://api.example.com/openapi.json',
173
- openApiSchemas,
174
- )
175
-
176
- const result2 = convertToLocalRef(
177
- 'https://api.example.com/schemas/user.json#/properties/name',
178
- 'https://api.example.com/openapi.json',
179
- openApiSchemas,
180
- )
181
-
182
- const result3 = convertToLocalRef(
183
- 'https://api.example.com/schemas/user.json#userProfile',
184
- 'https://api.example.com/openapi.json',
185
- openApiSchemas,
186
- )
187
-
188
- expect(result1).toBe('/components/schemas/User')
189
- expect(result2).toBe('/components/schemas/User/properties/name')
190
- expect(result3).toBe('/components/schemas/User/definitions/UserProfile')
191
- })
192
-
193
- it('should handle local component references in OpenAPI', () => {
194
- const openApiSchemas = new Map([
195
- ['https://api.example.com/openapi.json#userSchema', '/components/schemas/User'],
196
- ['https://api.example.com/openapi.json#addressSchema', '/components/schemas/Address'],
197
- ])
198
-
199
- const result1 = convertToLocalRef('#userSchema', 'https://api.example.com/openapi.json', openApiSchemas)
200
-
201
- const result2 = convertToLocalRef(
202
- '#/components/schemas/User/properties/name',
203
- 'https://api.example.com/openapi.json',
204
- openApiSchemas,
205
- )
206
-
207
- expect(result1).toBe('/components/schemas/User')
208
- expect(result2).toBe('components/schemas/User/properties/name')
209
- })
210
- })
211
- })
@@ -1,43 +0,0 @@
1
- /**
2
- * Translates a JSON Reference ($ref) to a local object path within the root schema.
3
- *
4
- * @param ref - The JSON Reference string (e.g., "#/foo/bar", "other.json#/baz", "other.json#anchor")
5
- * @param currentContext - The current base context (usually the $id of the current schema or parent)
6
- * @param schemas - A map of schema identifiers ($id, $anchor) to their local object paths
7
- * @returns The local object path as a string, or undefined if the reference cannot be resolved
8
- */
9
- export const convertToLocalRef = (
10
- ref: string,
11
- currentContext: string,
12
- schemas: Map<string, string>,
13
- ): string | undefined => {
14
- // Split the reference into base URL and path/anchor (e.g., "foo.json#/bar" => ["foo.json", "/bar"])
15
- const [baseUrl, pathOrAnchor] = ref.split('#', 2)
16
-
17
- if (baseUrl) {
18
- if (!schemas.has(baseUrl)) {
19
- return undefined
20
- }
21
-
22
- if (!pathOrAnchor) {
23
- return schemas.get(baseUrl)
24
- }
25
-
26
- // If the pathOrAnchor is a JSON pointer, we need to append it to the baseUrl
27
- if (pathOrAnchor.startsWith('/')) {
28
- return `${schemas.get(baseUrl)}${pathOrAnchor}`
29
- }
30
-
31
- // If the pathOrAnchor is an anchor, we need to return the anchor
32
- return schemas.get(`${baseUrl}#${pathOrAnchor}`)
33
- }
34
-
35
- if (pathOrAnchor) {
36
- if (pathOrAnchor.startsWith('/')) {
37
- return pathOrAnchor.slice(1)
38
- }
39
- return schemas.get(`${currentContext}#${pathOrAnchor}`)
40
- }
41
-
42
- return undefined
43
- }
@@ -1,13 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { escapeJsonPointer } from './escape-json-pointer'
4
-
5
- describe('escapeJsonPointer', () => {
6
- it('should escape a slash', () => {
7
- expect(escapeJsonPointer('application/json')).toBe('application~1json')
8
- })
9
-
10
- it('should escape multiple slashes', () => {
11
- expect(escapeJsonPointer('/api/users/{id}/reports')).toBe('~1api~1users~1{id}~1reports')
12
- })
13
- })
@@ -1,8 +0,0 @@
1
- /**
2
- * Escapes a JSON pointer string.
3
- *
4
- * Example: `/foo/bar~baz` -> `/foo~1bar~0baz`
5
- */
6
- export function escapeJsonPointer(str: string) {
7
- return str.replace(/~/g, '~0').replace(/\//g, '~1')
8
- }
@@ -1,356 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { getId, getSchemas } from './get-schemas'
4
-
5
- describe('getId', () => {
6
- it('returns the $id string when present', () => {
7
- const input = { $id: 'https://example.com/schema' }
8
- const result = getId(input)
9
- expect(result).toBe('https://example.com/schema')
10
- })
11
-
12
- it('returns undefined when $id is not present', () => {
13
- const input = { name: 'test' }
14
- const result = getId(input)
15
- expect(result).toBeUndefined()
16
- })
17
-
18
- it('returns undefined when $id is not a string', () => {
19
- const input = { $id: 123 }
20
- const result = getId(input)
21
- expect(result).toBeUndefined()
22
- })
23
-
24
- it('returns undefined when $id is null', () => {
25
- const input = { $id: null }
26
- const result = getId(input)
27
- expect(result).toBeUndefined()
28
- })
29
-
30
- it('returns undefined when $id is an object', () => {
31
- const input = { $id: { nested: 'value' } }
32
- const result = getId(input)
33
- expect(result).toBeUndefined()
34
- })
35
-
36
- it('returns undefined when $id is an array', () => {
37
- const input = { $id: ['item1', 'item2'] }
38
- const result = getId(input)
39
- expect(result).toBeUndefined()
40
- })
41
-
42
- it('returns undefined when $id is a boolean', () => {
43
- const input = { $id: true }
44
- const result = getId(input)
45
- expect(result).toBeUndefined()
46
- })
47
-
48
- it('returns undefined when $id is an empty string', () => {
49
- const input = { $id: '' }
50
- const result = getId(input)
51
- expect(result).toBe(undefined)
52
- })
53
- })
54
-
55
- describe('getSchemas', () => {
56
- it('returns empty map for non-object input', () => {
57
- const result = getSchemas('string')
58
- expect(result.size).toBe(0)
59
- })
60
-
61
- it('returns empty map for null input', () => {
62
- const result = getSchemas(null)
63
- expect(result.size).toBe(0)
64
- })
65
-
66
- it('returns empty map for undefined input', () => {
67
- const result = getSchemas(undefined)
68
- expect(result.size).toBe(0)
69
- })
70
-
71
- it('returns empty map for number input', () => {
72
- const result = getSchemas(42)
73
- expect(result.size).toBe(0)
74
- })
75
-
76
- it('returns empty map for boolean input', () => {
77
- const result = getSchemas(true)
78
- expect(result.size).toBe(0)
79
- })
80
-
81
- it('returns empty map for array input', () => {
82
- const result = getSchemas([1, 2, 3])
83
- expect(result.size).toBe(0)
84
- })
85
-
86
- it('collects schema with $id', () => {
87
- const input = {
88
- $id: 'https://example.com/schema',
89
- type: 'object',
90
- }
91
- const result = getSchemas(input)
92
- expect(result.size).toBe(1)
93
- expect(result.get('https://example.com/schema')).toBe('')
94
- })
95
-
96
- it('collects schema with $anchor', () => {
97
- const input = {
98
- $anchor: 'myAnchor',
99
- type: 'string',
100
- }
101
- const result = getSchemas(input)
102
- expect(result.size).toBe(1)
103
- expect(result.get('#myAnchor')).toBe('')
104
- })
105
-
106
- it('collects schema with both $id and $anchor', () => {
107
- const input = {
108
- $id: 'https://example.com/schema',
109
- $anchor: 'myAnchor',
110
- type: 'object',
111
- }
112
- const result = getSchemas(input)
113
- expect(result.size).toBe(2)
114
- expect(result.get('https://example.com/schema')).toBe('')
115
- expect(result.get('https://example.com/schema#myAnchor')).toBe('')
116
- })
117
-
118
- it('collects nested schemas with $id', () => {
119
- const input = {
120
- definitions: {
121
- user: {
122
- $id: 'https://example.com/user',
123
- type: 'object',
124
- },
125
- product: {
126
- $id: 'https://example.com/product',
127
- type: 'object',
128
- },
129
- },
130
- }
131
- const result = getSchemas(input)
132
- expect(result.size).toBe(2)
133
- expect(result.get('https://example.com/user')).toBe('definitions/user')
134
- expect(result.get('https://example.com/product')).toBe('definitions/product')
135
- })
136
-
137
- it('collects nested schemas with $anchor', () => {
138
- const input = {
139
- definitions: {
140
- user: {
141
- $anchor: 'user',
142
- type: 'object',
143
- },
144
- product: {
145
- $anchor: 'product',
146
- type: 'object',
147
- },
148
- },
149
- }
150
- const result = getSchemas(input)
151
- expect(result.size).toBe(2)
152
- expect(result.get('#user')).toBe('definitions/user')
153
- expect(result.get('#product')).toBe('definitions/product')
154
- })
155
-
156
- it('uses parent $id as base for nested $anchor', () => {
157
- const input = {
158
- $id: 'https://example.com/schema',
159
- definitions: {
160
- user: {
161
- $anchor: 'user',
162
- type: 'object',
163
- },
164
- },
165
- }
166
- const result = getSchemas(input)
167
- expect(result.size).toBe(2)
168
- expect(result.get('https://example.com/schema')).toBe('')
169
- expect(result.get('https://example.com/schema#user')).toBe('definitions/user')
170
- })
171
-
172
- it('handles deeply nested schemas', () => {
173
- const input = {
174
- components: {
175
- schemas: {
176
- user: {
177
- $id: 'https://example.com/user',
178
- properties: {
179
- address: {
180
- $anchor: 'address',
181
- type: 'object',
182
- },
183
- },
184
- },
185
- },
186
- },
187
- }
188
- const result = getSchemas(input)
189
- expect(result.size).toBe(2)
190
- expect(result.get('https://example.com/user')).toBe('components/schemas/user')
191
- expect(result.get('https://example.com/user#address')).toBe('components/schemas/user/properties/address')
192
- })
193
-
194
- it('handles circular references without infinite loops', () => {
195
- const input: any = {
196
- $id: 'https://example.com/schema',
197
- type: 'object',
198
- }
199
- input.self = input // Create circular reference
200
-
201
- const result = getSchemas(input)
202
- expect(result.size).toBe(1)
203
- expect(result.get('https://example.com/schema')).toBe('')
204
- })
205
-
206
- it('handles multiple circular references', () => {
207
- const input: any = {
208
- $id: 'https://example.com/schema',
209
- type: 'object',
210
- properties: {
211
- user: {
212
- $anchor: 'user',
213
- type: 'object',
214
- },
215
- },
216
- }
217
- input.self = input
218
- input.properties.user.self = input
219
-
220
- const result = getSchemas(input)
221
- expect(result.size).toBe(2)
222
- expect(result.get('https://example.com/schema')).toBe('')
223
- expect(result.get('https://example.com/schema#user')).toBe('properties/user')
224
- })
225
-
226
- it('ignores non-string $id values', () => {
227
- const input = {
228
- $id: 123,
229
- type: 'object',
230
- }
231
- const result = getSchemas(input)
232
- expect(result.size).toBe(0)
233
- })
234
-
235
- it('ignores non-string $anchor values', () => {
236
- const input = {
237
- $anchor: 123,
238
- type: 'object',
239
- }
240
- const result = getSchemas(input)
241
- expect(result.size).toBe(0)
242
- })
243
-
244
- it('handles empty objects', () => {
245
- const input = {}
246
- const result = getSchemas(input)
247
- expect(result.size).toBe(0)
248
- })
249
-
250
- it('handles objects with only primitive values', () => {
251
- const input = {
252
- name: 'test',
253
- age: 25,
254
- active: true,
255
- tags: ['tag1', 'tag2'],
256
- }
257
- const result = getSchemas(input)
258
- expect(result.size).toBe(0)
259
- })
260
-
261
- it('handles mixed primitive and object values', () => {
262
- const input = {
263
- name: 'test',
264
- schema: {
265
- $id: 'https://example.com/schema',
266
- type: 'object',
267
- },
268
- age: 25,
269
- }
270
- const result = getSchemas(input)
271
- expect(result.size).toBe(1)
272
- expect(result.get('https://example.com/schema')).toBe('schema')
273
- })
274
-
275
- it('preserves custom base parameter', () => {
276
- const input = {
277
- $anchor: 'myAnchor',
278
- type: 'object',
279
- }
280
- const result = getSchemas(input, 'https://custom.com/base')
281
- expect(result.size).toBe(1)
282
- expect(result.get('https://custom.com/base#myAnchor')).toBe('')
283
- })
284
-
285
- it('preserves custom segments parameter', () => {
286
- const input = {
287
- $id: 'https://example.com/schema',
288
- type: 'object',
289
- }
290
- const result = getSchemas(input, '', ['custom', 'path'])
291
- expect(result.size).toBe(1)
292
- expect(result.get('https://example.com/schema')).toBe('custom/path')
293
- })
294
-
295
- it('reuses existing map when provided', () => {
296
- const existingMap = new Map([['existing', 'path']])
297
- const input = {
298
- $id: 'https://example.com/schema',
299
- type: 'object',
300
- }
301
- const result = getSchemas(input, '', [], existingMap)
302
- expect(result).toBe(existingMap)
303
- expect(result.size).toBe(2)
304
- expect(result.get('existing')).toBe('path')
305
- expect(result.get('https://example.com/schema')).toBe('')
306
- })
307
-
308
- it('handles complex nested structure with multiple schemas', () => {
309
- const input = {
310
- $id: 'https://example.com/root',
311
- openapi: '3.0.0',
312
- info: {
313
- title: 'API',
314
- },
315
- components: {
316
- schemas: {
317
- user: {
318
- $id: 'https://example.com/user',
319
- $anchor: 'user',
320
- type: 'object',
321
- properties: {
322
- name: {
323
- type: 'string',
324
- },
325
- address: {
326
- $anchor: 'address',
327
- type: 'object',
328
- properties: {
329
- street: {
330
- type: 'string',
331
- },
332
- },
333
- },
334
- },
335
- },
336
- product: {
337
- $anchor: 'product',
338
- type: 'object',
339
- properties: {
340
- id: {
341
- type: 'integer',
342
- },
343
- },
344
- },
345
- },
346
- },
347
- }
348
- const result = getSchemas(input)
349
- expect(result.size).toBe(5)
350
- expect(result.get('https://example.com/root')).toBe('')
351
- expect(result.get('https://example.com/user')).toBe('components/schemas/user')
352
- expect(result.get('https://example.com/user#user')).toBe('components/schemas/user')
353
- expect(result.get('https://example.com/user#address')).toBe('components/schemas/user/properties/address')
354
- expect(result.get('https://example.com/root#product')).toBe('components/schemas/product')
355
- })
356
- })