fast-json-stringify 5.11.0 → 5.12.0

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/index.js CHANGED
@@ -29,8 +29,6 @@ const validLargeArrayMechanisms = [
29
29
 
30
30
  let schemaIdCounter = 0
31
31
 
32
- const mergedSchemaRef = Symbol('fjs-merged-schema-ref')
33
-
34
32
  function isValidSchema (schema, name) {
35
33
  if (!validate(schema)) {
36
34
  if (name) {
@@ -93,7 +91,8 @@ function build (schema, options) {
93
91
  options,
94
92
  refResolver: new RefResolver(),
95
93
  rootSchemaId: schema.$id || `__fjs_root_${schemaIdCounter++}`,
96
- validatorSchemasIds: new Set()
94
+ validatorSchemasIds: new Set(),
95
+ mergedSchemasIds: new Map()
97
96
  }
98
97
 
99
98
  const schemaId = getSchemaId(schema, context.rootSchemaId)
@@ -408,7 +407,7 @@ function mergeLocations (context, mergedSchemaId, mergedLocations) {
408
407
 
409
408
  const mergedSchemas = []
410
409
  for (const location of mergedLocations) {
411
- const schema = cloneOriginSchema(location.schema, location.schemaId)
410
+ const schema = cloneOriginSchema(context, location.schema, location.schemaId)
412
411
  delete schema.$id
413
412
 
414
413
  mergedSchemas.push(schema)
@@ -421,7 +420,7 @@ function mergeLocations (context, mergedSchemaId, mergedLocations) {
421
420
  return mergedLocation
422
421
  }
423
422
 
424
- function cloneOriginSchema (schema, schemaId) {
423
+ function cloneOriginSchema (context, schema, schemaId) {
425
424
  const clonedSchema = Array.isArray(schema) ? [] : {}
426
425
 
427
426
  if (
@@ -431,8 +430,9 @@ function cloneOriginSchema (schema, schemaId) {
431
430
  schemaId = schema.$id
432
431
  }
433
432
 
434
- if (schema[mergedSchemaRef]) {
435
- clonedSchema[mergedSchemaRef] = schema[mergedSchemaRef]
433
+ const mergedSchemaRef = context.mergedSchemasIds.get(schema)
434
+ if (mergedSchemaRef) {
435
+ context.mergedSchemasIds.set(clonedSchema, mergedSchemaRef)
436
436
  }
437
437
 
438
438
  for (const key in schema) {
@@ -443,7 +443,7 @@ function cloneOriginSchema (schema, schemaId) {
443
443
  }
444
444
 
445
445
  if (typeof value === 'object' && value !== null) {
446
- value = cloneOriginSchema(value, schemaId)
446
+ value = cloneOriginSchema(context, value, schemaId)
447
447
  }
448
448
 
449
449
  clonedSchema[key] = value
@@ -780,14 +780,14 @@ function buildConstSerializer (location, input) {
780
780
  function buildAllOf (context, location, input) {
781
781
  const schema = location.schema
782
782
 
783
- let mergedSchemaId = schema[mergedSchemaRef]
783
+ let mergedSchemaId = context.mergedSchemasIds.get(schema)
784
784
  if (mergedSchemaId) {
785
785
  const mergedLocation = getMergedLocation(context, mergedSchemaId)
786
786
  return buildValue(context, mergedLocation, input)
787
787
  }
788
788
 
789
789
  mergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
790
- schema[mergedSchemaRef] = mergedSchemaId
790
+ context.mergedSchemasIds.set(schema, mergedSchemaId)
791
791
 
792
792
  const { allOf, ...schemaWithoutAllOf } = location.schema
793
793
  const locations = [
@@ -828,13 +828,13 @@ function buildOneOf (context, location, input) {
828
828
  const optionLocation = oneOfsLocation.getPropertyLocation(index)
829
829
  const optionSchema = optionLocation.schema
830
830
 
831
- let mergedSchemaId = optionSchema[mergedSchemaRef]
831
+ let mergedSchemaId = context.mergedSchemasIds.get(optionSchema)
832
832
  let mergedLocation = null
833
833
  if (mergedSchemaId) {
834
834
  mergedLocation = getMergedLocation(context, mergedSchemaId)
835
835
  } else {
836
836
  mergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
837
- optionSchema[mergedSchemaRef] = mergedSchemaId
837
+ context.mergedSchemasIds.set(optionSchema, mergedSchemaId)
838
838
 
839
839
  mergedLocation = mergeLocations(context, mergedSchemaId, [
840
840
  locationWithoutOneOf,
@@ -882,13 +882,13 @@ function buildIfThenElse (context, location, input) {
882
882
  const ifSchemaRef = ifLocation.getSchemaRef()
883
883
 
884
884
  const thenLocation = location.getPropertyLocation('then')
885
- let thenMergedSchemaId = thenSchema[mergedSchemaRef]
885
+ let thenMergedSchemaId = context.mergedSchemasIds.get(thenSchema)
886
886
  let thenMergedLocation = null
887
887
  if (thenMergedSchemaId) {
888
888
  thenMergedLocation = getMergedLocation(context, thenMergedSchemaId)
889
889
  } else {
890
890
  thenMergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
891
- thenSchema[mergedSchemaRef] = thenMergedSchemaId
891
+ context.mergedSchemasIds.set(thenSchema, thenMergedSchemaId)
892
892
 
893
893
  thenMergedLocation = mergeLocations(context, thenMergedSchemaId, [
894
894
  rootLocation,
@@ -907,13 +907,13 @@ function buildIfThenElse (context, location, input) {
907
907
  }
908
908
 
909
909
  const elseLocation = location.getPropertyLocation('else')
910
- let elseMergedSchemaId = elseSchema[mergedSchemaRef]
910
+ let elseMergedSchemaId = context.mergedSchemasIds.get(elseSchema)
911
911
  let elseMergedLocation = null
912
912
  if (elseMergedSchemaId) {
913
913
  elseMergedLocation = getMergedLocation(context, elseMergedSchemaId)
914
914
  } else {
915
915
  elseMergedSchemaId = `__fjs_merged_${schemaIdCounter++}`
916
- elseSchema[mergedSchemaRef] = elseMergedSchemaId
916
+ context.mergedSchemasIds.set(elseSchema, elseMergedSchemaId)
917
917
 
918
918
  elseMergedLocation = mergeLocations(context, elseMergedSchemaId, [
919
919
  rootLocation,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "5.11.0",
3
+ "version": "5.12.0",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -765,3 +765,30 @@ test('external recursive anyOfs', (t) => {
765
765
  const stringify = build(schema, { schema: { externalSchema } })
766
766
  t.equal(stringify(data), '{"a":{"bar":"42","foo":{}},"b":{"bar":"42","foo":{}}}')
767
767
  })
768
+
769
+ test('should build merged schemas twice', (t) => {
770
+ t.plan(2)
771
+
772
+ const schema = {
773
+ type: 'object',
774
+ properties: {
775
+ enums: {
776
+ type: 'string',
777
+ anyOf: [
778
+ { type: 'string', const: 'FOO' },
779
+ { type: 'string', const: 'BAR' }
780
+ ]
781
+ }
782
+ }
783
+ }
784
+
785
+ {
786
+ const stringify = build(schema)
787
+ t.equal(stringify({ enums: 'FOO' }), '{"enums":"FOO"}')
788
+ }
789
+
790
+ {
791
+ const stringify = build(schema)
792
+ t.equal(stringify({ enums: 'BAR' }), '{"enums":"BAR"}')
793
+ }
794
+ })