@@ -832,11 +832,19 @@ function toJSON (variableName) {
832
832
`
833
833
}
834
834
835
- function buildObject ( location , functionName , locationPath ) {
835
+ function buildObject ( location , locationPath ) {
836
836
const schema = location . schema
837
837
if ( schema . $id !== undefined ) {
838
838
schemaReferenceMap . set ( schema . $id , schema )
839
839
}
840
+
841
+ if ( objectReferenceSerializersMap . has ( schema ) ) {
842
+ return objectReferenceSerializersMap . get ( schema )
843
+ }
844
+
845
+ const functionName = generateFuncName ( )
846
+ objectReferenceSerializersMap . set ( schema , functionName )
847
+
840
848
let functionCode = `
841
849
function ${ functionName } (input) {
842
850
// ${ locationPath }
@@ -849,16 +857,6 @@ function buildObject (location, functionName, locationPath) {
849
857
`
850
858
}
851
859
852
- if ( objectReferenceSerializersMap . has ( schema ) && objectReferenceSerializersMap . get ( schema ) !== functionName ) {
853
- functionCode += `
854
- return ${ objectReferenceSerializersMap . get ( schema ) } (input)
855
- }
856
- `
857
- contextFunctions . push ( functionCode )
858
- return
859
- }
860
- objectReferenceSerializersMap . set ( schema , functionName )
861
-
862
860
functionCode += `
863
861
var obj = ${ toJSON ( 'input' ) }
864
862
var json = '{'
@@ -883,24 +881,14 @@ function buildObject (location, functionName, locationPath) {
883
881
`
884
882
885
883
contextFunctions . push ( functionCode )
884
+ return functionName
886
885
}
887
886
888
- function buildArray ( location , functionName , locationPath ) {
887
+ function buildArray ( location , locationPath ) {
889
888
let schema = location . schema
890
889
if ( schema . $id !== undefined ) {
891
890
schemaReferenceMap . set ( schema . $id , schema )
892
891
}
893
- let functionCode = `
894
- function ${ functionName } (obj) {
895
- // ${ locationPath }
896
- `
897
- if ( schema . nullable ) {
898
- functionCode += `
899
- if(obj === null) {
900
- return 'null';
901
- }
902
- `
903
- }
904
892
905
893
// default to any items type
906
894
if ( ! schema . items ) {
@@ -916,16 +904,26 @@ function buildArray (location, functionName, locationPath) {
916
904
917
905
location = refFinder ( schema . items . $ref , location )
918
906
schema . items = location . schema
907
+ }
908
+
909
+ if ( arrayItemsReferenceSerializersMap . has ( schema . items ) ) {
910
+ return arrayItemsReferenceSerializersMap . get ( schema . items )
911
+ }
912
+
913
+ const functionName = generateFuncName ( )
914
+ arrayItemsReferenceSerializersMap . set ( schema . items , functionName )
915
+
916
+ let functionCode = `
917
+ function ${ functionName } (obj) {
918
+ // ${ locationPath }
919
+ `
919
920
920
- if ( arrayItemsReferenceSerializersMap . has ( schema . items ) ) {
921
- functionCode += `
922
- return ${ arrayItemsReferenceSerializersMap . get ( schema . items ) } (obj)
921
+ if ( schema . nullable ) {
922
+ functionCode += `
923
+ if (obj === null) {
924
+ return 'null';
923
925
}
924
- `
925
- contextFunctions . push ( functionCode )
926
- return
927
- }
928
- arrayItemsReferenceSerializersMap . set ( schema . items , functionName )
926
+ `
929
927
}
930
928
931
929
let result = { code : '' }
@@ -990,6 +988,7 @@ function buildArray (location, functionName, locationPath) {
990
988
}`
991
989
992
990
contextFunctions . push ( functionCode )
991
+ return functionName
993
992
}
994
993
995
994
function buildArrayTypeCondition ( type , accessor ) {
@@ -1109,13 +1108,11 @@ function buildValue (locationPath, input, location) {
1109
1108
code += `json += ${ funcName } (${ input } )`
1110
1109
break
1111
1110
case 'object' :
1112
- funcName = generateFuncName ( )
1113
- buildObject ( location , funcName , locationPath )
1111
+ funcName = buildObject ( location , locationPath )
1114
1112
code += `json += ${ funcName } (${ input } )`
1115
1113
break
1116
1114
case 'array' :
1117
- funcName = generateFuncName ( )
1118
- buildArray ( location , funcName , locationPath )
1115
+ funcName = buildArray ( location , locationPath )
1119
1116
code += `json += ${ funcName } (${ input } )`
1120
1117
break
1121
1118
case undefined :
0 commit comments