File tree Expand file tree Collapse file tree 3 files changed +100
-2
lines changed
src/core/plugins/json-schema-2020-12-samples/fn
test/unit/core/plugins/json-schema-2020-12-samples Expand file tree Collapse file tree 3 files changed +100
-2
lines changed Original file line number Diff line number Diff line change @@ -148,7 +148,31 @@ export const sampleFromSchemaGeneric = (
148
148
const propSchema = typeCast ( props [ propName ] )
149
149
const propSchemaType = getType ( propSchema )
150
150
const attrName = props [ propName ] . xml . name || propName
151
- _attr [ attrName ] = typeMap [ propSchemaType ] ( propSchema )
151
+
152
+ if ( propSchemaType === "array" ) {
153
+ const arraySample = sampleFromSchemaGeneric (
154
+ props [ propName ] ,
155
+ config ,
156
+ overrideE ,
157
+ false
158
+ )
159
+ _attr [ attrName ] = arraySample
160
+ . map ( ( item ) => {
161
+ if ( isPlainObject ( item ) ) {
162
+ return "UnknownTypeObject"
163
+ }
164
+ if ( Array . isArray ( item ) ) {
165
+ return "UnknownTypeArray"
166
+ }
167
+ return item
168
+ } )
169
+ . join ( " " )
170
+ } else {
171
+ _attr [ attrName ] =
172
+ propSchemaType === "object"
173
+ ? "UnknownTypeObject"
174
+ : typeMap [ propSchemaType ] ( propSchema )
175
+ }
152
176
}
153
177
154
178
return
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ export const applyArrayConstraints = (array, constraints = {}) => {
45
45
return constrainedArray
46
46
}
47
47
48
- const arrayType = ( schema , { sample } ) => {
48
+ const arrayType = ( schema , { sample = [ ] } = { } ) => {
49
49
return applyArrayConstraints ( sample , schema )
50
50
}
51
51
Original file line number Diff line number Diff line change @@ -2954,6 +2954,80 @@ describe("createXMLExample", function () {
2954
2954
2955
2955
expect ( sut ( definition ) ) . toEqual ( expected )
2956
2956
} )
2957
+
2958
+ it ( "should handle object properties of type `array` as an attribute" , ( ) => {
2959
+ const definition = {
2960
+ type : "object" ,
2961
+ xml : {
2962
+ name : "test" ,
2963
+ } ,
2964
+ properties : {
2965
+ arrayOfStrings : {
2966
+ type : "array" ,
2967
+ items : {
2968
+ type : "string" ,
2969
+ } ,
2970
+ xml : {
2971
+ attribute : true ,
2972
+ } ,
2973
+ } ,
2974
+ arrayOfArrays : {
2975
+ type : "array" ,
2976
+ items : {
2977
+ type : "array" ,
2978
+ } ,
2979
+ minItems : 3 ,
2980
+ xml : {
2981
+ attribute : true ,
2982
+ } ,
2983
+ } ,
2984
+ arrayOfContainsObject : {
2985
+ type : "array" ,
2986
+ contains : {
2987
+ type : "object" ,
2988
+ } ,
2989
+ minContains : 3 ,
2990
+ xml : {
2991
+ attribute : true ,
2992
+ } ,
2993
+ } ,
2994
+ } ,
2995
+ }
2996
+
2997
+ const expected = `<?xml version="1.0" encoding="UTF-8"?>
2998
+ <test arrayOfStrings="string" arrayOfArrays="UnknownTypeArray UnknownTypeArray UnknownTypeArray" arrayOfContainsObject="UnknownTypeObject UnknownTypeObject UnknownTypeObject">
2999
+ </test>`
3000
+
3001
+ expect ( sut ( definition ) ) . toEqual ( expected )
3002
+ } )
3003
+
3004
+ it ( "should handle object properties of type `object` as an attribute" , ( ) => {
3005
+ const definition = {
3006
+ type : "object" ,
3007
+ xml : {
3008
+ name : "test" ,
3009
+ } ,
3010
+ properties : {
3011
+ object : {
3012
+ type : "object" ,
3013
+ properties : {
3014
+ string : {
3015
+ type : "string" ,
3016
+ } ,
3017
+ } ,
3018
+ xml : {
3019
+ attribute : true ,
3020
+ } ,
3021
+ } ,
3022
+ } ,
3023
+ }
3024
+
3025
+ const expected = `<?xml version="1.0" encoding="UTF-8"?>
3026
+ <test object="UnknownTypeObject">
3027
+ </test>`
3028
+
3029
+ expect ( sut ( definition ) ) . toEqual ( expected )
3030
+ } )
2957
3031
} )
2958
3032
2959
3033
describe ( "memoizedSampleFromSchema" , ( ) => {
You can’t perform that action at this time.
0 commit comments