File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -37,6 +37,34 @@ defmodule Serializer do
3737 def to_string ( socket_tree ) do
3838 socket_tree
3939 |> to_schema ( )
40+ |> to_ordered_structs ( )
4041 |> Jason . encode! ( pretty: true )
4142 end
43+
44+ # Helper function to ensure the order of the output json
45+ @ spec to_ordered_structs ( map ( ) ) :: Jason.OrderedObject . t ( )
46+ defp to_ordered_structs ( map ) when is_map ( map ) do
47+ reverse_ordered =
48+ map
49+ |> Enum . to_list ( )
50+ |> Enum . sort_by ( fn { key , _val } -> key end , :asc )
51+ |> Enum . reduce ( % Jason.OrderedObject { } , fn
52+ { key , value } , acc when is_map ( value ) ->
53+ % { acc | values: [ { key , to_ordered_structs ( value ) } | acc . values ] }
54+
55+ { key , value } , acc when is_list ( value ) ->
56+ % { acc | values: [ { key , to_ordered_structs ( value ) } | acc . values ] }
57+
58+ { key , value } , acc ->
59+ % { acc | values: [ { key , value } | acc . values ] }
60+ end )
61+
62+ % { reverse_ordered | values: Enum . reverse ( reverse_ordered . values ) }
63+ end
64+
65+ defp to_ordered_structs ( list ) when is_list ( list ) do
66+ Enum . map ( list , & to_ordered_structs / 1 )
67+ end
68+
69+ defp to_ordered_structs ( stuff ) , do: stuff
4270end
Original file line number Diff line number Diff line change @@ -38,5 +38,226 @@ defmodule ChannelSpec.SerializerTest do
3838 "type" => "object"
3939 }
4040 end
41+
42+ test "foo" do
43+ schema = % {
44+ type: :object ,
45+ properties: % {
46+ b: % { type: :string } ,
47+ c: % { type: :string } ,
48+ x: % { type: :string } ,
49+ d: % { type: :string } ,
50+ electric_eel: % { type: :string } ,
51+ e: % { type: :string } ,
52+ h: % { type: :string } ,
53+ f: % { type: :string } ,
54+ g: % { type: :string } ,
55+ dog: % { type: :string } ,
56+ j: % { type: :string } ,
57+ k: % { type: :string } ,
58+ o: % { type: :string } ,
59+ jaguar: % { type: :string } ,
60+ l: % { type: :string } ,
61+ lemur: % { type: :string } ,
62+ u: % { type: :string } ,
63+ cuttle_fish: % { type: :string } ,
64+ m: % { type: :string } ,
65+ n: % { type: :string } ,
66+ ibex: % { type: :string } ,
67+ i: % { type: :string } ,
68+ hippopotamus: % { type: :string } ,
69+ y: % { type: :string } ,
70+ fox: % { type: :string } ,
71+ q: % { type: :string } ,
72+ a: % { type: :string } ,
73+ z: % { type: :string } ,
74+ koala: % { type: :string } ,
75+ r: % { type: :string } ,
76+ garden_snake: % { type: :string } ,
77+ s: % { type: :string } ,
78+ p: % { type: :string } ,
79+ bananas: % { type: :string } ,
80+ v: % { type: :string } ,
81+ t: % { type: :string } ,
82+ w: % { type: :string } ,
83+ apples: % { type: :string } ,
84+ one_of_list: [
85+ % {
86+ type: :object ,
87+ properties: % {
88+ c: % { type: :string } ,
89+ b: % { type: :string } ,
90+ a: % { type: :string }
91+ }
92+ } ,
93+ % {
94+ type: :object ,
95+ properties: % {
96+ f: % { type: :string } ,
97+ e: % { type: :string } ,
98+ d: % { type: :string }
99+ }
100+ }
101+ ]
102+ }
103+ }
104+
105+ result =
106+ schema
107+ |> Serializer . to_schema ( )
108+ |> Serializer . to_string ( )
109+
110+ assert """
111+ {
112+ "properties": {
113+ "a": {
114+ "type": "string"
115+ },
116+ "apples": {
117+ "type": "string"
118+ },
119+ "b": {
120+ "type": "string"
121+ },
122+ "bananas": {
123+ "type": "string"
124+ },
125+ "c": {
126+ "type": "string"
127+ },
128+ "cuttle_fish": {
129+ "type": "string"
130+ },
131+ "d": {
132+ "type": "string"
133+ },
134+ "dog": {
135+ "type": "string"
136+ },
137+ "e": {
138+ "type": "string"
139+ },
140+ "electric_eel": {
141+ "type": "string"
142+ },
143+ "f": {
144+ "type": "string"
145+ },
146+ "fox": {
147+ "type": "string"
148+ },
149+ "g": {
150+ "type": "string"
151+ },
152+ "garden_snake": {
153+ "type": "string"
154+ },
155+ "h": {
156+ "type": "string"
157+ },
158+ "hippopotamus": {
159+ "type": "string"
160+ },
161+ "i": {
162+ "type": "string"
163+ },
164+ "ibex": {
165+ "type": "string"
166+ },
167+ "j": {
168+ "type": "string"
169+ },
170+ "jaguar": {
171+ "type": "string"
172+ },
173+ "k": {
174+ "type": "string"
175+ },
176+ "koala": {
177+ "type": "string"
178+ },
179+ "l": {
180+ "type": "string"
181+ },
182+ "lemur": {
183+ "type": "string"
184+ },
185+ "m": {
186+ "type": "string"
187+ },
188+ "n": {
189+ "type": "string"
190+ },
191+ "o": {
192+ "type": "string"
193+ },
194+ "one_of_list": [
195+ {
196+ "properties": {
197+ "a": {
198+ "type": "string"
199+ },
200+ "b": {
201+ "type": "string"
202+ },
203+ "c": {
204+ "type": "string"
205+ }
206+ },
207+ "type": "object"
208+ },
209+ {
210+ "properties": {
211+ "d": {
212+ "type": "string"
213+ },
214+ "e": {
215+ "type": "string"
216+ },
217+ "f": {
218+ "type": "string"
219+ }
220+ },
221+ "type": "object"
222+ }
223+ ],
224+ "p": {
225+ "type": "string"
226+ },
227+ "q": {
228+ "type": "string"
229+ },
230+ "r": {
231+ "type": "string"
232+ },
233+ "s": {
234+ "type": "string"
235+ },
236+ "t": {
237+ "type": "string"
238+ },
239+ "u": {
240+ "type": "string"
241+ },
242+ "v": {
243+ "type": "string"
244+ },
245+ "w": {
246+ "type": "string"
247+ },
248+ "x": {
249+ "type": "string"
250+ },
251+ "y": {
252+ "type": "string"
253+ },
254+ "z": {
255+ "type": "string"
256+ }
257+ },
258+ "type": "object"
259+ }\
260+ """ == result
261+ end
41262 end
42263end
You can’t perform that action at this time.
0 commit comments