11package com .retailsvc .http .spec .schema ;
22
33import java .util .EnumSet ;
4+ import java .util .LinkedHashMap ;
45import java .util .List ;
56import java .util .Map ;
67import java .util .Set ;
@@ -26,8 +27,8 @@ public static Schema parse(Map<String, Object> raw) {
2627 case NUMBER -> parseNumber (raw , types );
2728 case BOOLEAN -> new BooleanSchema (types );
2829 case NULL -> new NullSchema ();
29- case OBJECT , ARRAY ->
30- throw new UnsupportedOperationException ( "object/array parsing comes in C2" );
30+ case OBJECT -> parseObject ( raw , types );
31+ case ARRAY -> parseArray ( raw , types );
3132 };
3233 }
3334
@@ -80,6 +81,47 @@ private static NumberSchema parseNumber(Map<String, Object> raw, Set<TypeName> t
8081 (String ) raw .get ("format" ));
8182 }
8283
84+ @ SuppressWarnings ("unchecked" )
85+ private static ObjectSchema parseObject (Map <String , Object > raw , Set <TypeName > types ) {
86+ Map <String , Object > rawProps = (Map <String , Object >) raw .getOrDefault ("properties" , Map .of ());
87+ Map <String , Schema > properties = new LinkedHashMap <>();
88+ for (var e : rawProps .entrySet ()) {
89+ properties .put (e .getKey (), parse ((Map <String , Object >) e .getValue ()));
90+ }
91+ List <String > required = (List <String >) raw .getOrDefault ("required" , List .of ());
92+ AdditionalProperties ap = parseAdditionalProperties (raw .get ("additionalProperties" ));
93+ return new ObjectSchema (
94+ types ,
95+ Map .copyOf (properties ),
96+ List .copyOf (required ),
97+ ap ,
98+ toIntOrNull (raw .get ("minProperties" )),
99+ toIntOrNull (raw .get ("maxProperties" )));
100+ }
101+
102+ @ SuppressWarnings ("unchecked" )
103+ private static AdditionalProperties parseAdditionalProperties (Object value ) {
104+ if (value == null || Boolean .TRUE .equals (value )) {
105+ return new AdditionalProperties .Allowed ();
106+ }
107+ if (Boolean .FALSE .equals (value )) {
108+ return new AdditionalProperties .Forbidden ();
109+ }
110+ return new AdditionalProperties .SchemaConstraint (parse ((Map <String , Object >) value ));
111+ }
112+
113+ @ SuppressWarnings ("unchecked" )
114+ private static ArraySchema parseArray (Map <String , Object > raw , Set <TypeName > types ) {
115+ Map <String , Object > items = (Map <String , Object >) raw .getOrDefault ("items" , Map .of ());
116+ Schema itemSchema = items .isEmpty () ? new NullSchema () : parse (items );
117+ return new ArraySchema (
118+ types ,
119+ itemSchema ,
120+ toIntOrNull (raw .get ("minItems" )),
121+ toIntOrNull (raw .get ("maxItems" )),
122+ Boolean .TRUE .equals (raw .get ("uniqueItems" )));
123+ }
124+
83125 private static Integer toIntOrNull (Object v ) {
84126 return v == null ? null : ((Number ) v ).intValue ();
85127 }
0 commit comments