@@ -151,6 +151,29 @@ def _is_macro_schema(path: Path) -> bool:
151151 )
152152
153153
154+ def normalize_enum_descriptions (obj ):
155+ """Normalize OpenAPI-style enum description maps for code generation.
156+
157+ AdCP schemas key ``x-enum-descriptions`` by enum value. Newer
158+ datamodel-code-generator releases validate that extension as a positional
159+ list, so translate the map in enum order in the temporary schema tree.
160+ The published schema cache remains byte-for-byte unchanged.
161+ """
162+ if isinstance (obj , dict ):
163+ descriptions = obj .get ("x-enum-descriptions" )
164+ enum_values = obj .get ("enum" )
165+ if isinstance (descriptions , dict ) and isinstance (enum_values , list ):
166+ obj ["x-enum-descriptions" ] = [
167+ str (descriptions .get (str (enum_value ), "" )) for enum_value in enum_values
168+ ]
169+ for value in obj .values ():
170+ normalize_enum_descriptions (value )
171+ elif isinstance (obj , list ):
172+ for item in obj :
173+ normalize_enum_descriptions (item )
174+ return obj
175+
176+
154177def rewrite_refs (obj , current_schema_rel_path : Path ):
155178 """
156179 Recursively rewrite $ref paths:
@@ -303,8 +326,20 @@ def flatten_validation_oneof(schema: dict) -> dict:
303326 if not branches :
304327 return schema
305328
306- # All branches must contain only 'required' (and optionally 'not')
307- if not all (set (b .keys ()) <= {"required" , "not" } for b in branches ):
329+ # Alternative annotations do not turn a required-field constraint into a
330+ # distinct object shape.
331+ validation_branch_keys = {
332+ "$comment" ,
333+ "deprecated" ,
334+ "description" ,
335+ "examples" ,
336+ "not" ,
337+ "required" ,
338+ "title" ,
339+ }
340+ if not all (
341+ isinstance (branch , dict ) and set (branch ) <= validation_branch_keys for branch in branches
342+ ):
308343 return schema
309344
310345 # All branches are required-only — this is a validation constraint, not a type union
@@ -332,6 +367,75 @@ def flatten_validation_oneof(schema: dict) -> dict:
332367 return schema
333368
334369
370+ _ROOT_OBJECT_VALIDATION_UNIONS = {
371+ Path ("account/list-account-changes-response.json" ),
372+ Path ("brand/search-brands-response.json" ),
373+ Path ("brand/verify-brand-claim-request.json" ),
374+ Path ("core/product.json" ),
375+ Path ("media-buy/buy-products-request.json" ),
376+ Path ("media-buy/create-media-buy-request.json" ),
377+ Path ("media-buy/get-reporting-status-response.json" ),
378+ Path ("media-buy/request-proposals-request.json" ),
379+ }
380+
381+
382+ def flatten_root_object_validation_union (schema : dict , schema_path : Path ) -> dict :
383+ """Keep known root object overlays as concrete, subclassable models.
384+
385+ These schemas put their shared object envelope at the root and use a
386+ root-level ``oneOf``/``anyOf`` only for cross-field validation. codegen
387+ 0.63+ emits a ``RootModel`` union for that pattern, which breaks the SDK's
388+ public subclassability contract. Merge branch-only fields conservatively;
389+ nested unions remain untouched and runtime validators continue to enforce
390+ the cross-field rules that the older generator also could not express.
391+ """
392+ if schema_path not in _ROOT_OBJECT_VALIDATION_UNIONS or schema .get ("type" ) != "object" :
393+ return schema
394+
395+ branch_key = next ((key for key in ("anyOf" , "oneOf" ) if key in schema ), None )
396+ if branch_key is None :
397+ return schema
398+ branches = schema [branch_key ]
399+ if (
400+ not isinstance (branches , list )
401+ or not branches
402+ or not all (isinstance (branch , dict ) for branch in branches )
403+ ):
404+ return schema
405+
406+ properties = dict (schema .get ("properties" , {}))
407+ branch_properties : dict [str , list [dict ]] = {}
408+ for branch in branches :
409+ for name , definition in branch .get ("properties" , {}).items ():
410+ if name not in properties and isinstance (definition , dict ):
411+ branch_properties .setdefault (name , []).append (definition )
412+
413+ for name , definitions in branch_properties .items ():
414+ unique = {json .dumps (definition , sort_keys = True ): definition for definition in definitions }
415+ variants = list (unique .values ())
416+ if len (variants ) == 1 :
417+ properties [name ] = variants [0 ]
418+ elif all ("const" in variant for variant in variants ):
419+ properties [name ] = {"enum" : [variant ["const" ] for variant in variants ]}
420+ elif all (variant .get ("type" ) == "object" for variant in variants ):
421+ properties [name ] = {"type" : "object" , "additionalProperties" : True }
422+ else :
423+ properties [name ] = {"anyOf" : variants }
424+
425+ schema ["properties" ] = properties
426+ top_required = set (schema .get ("required" , []))
427+ branch_required = [set (branch .get ("required" , [])) for branch in branches ]
428+ common_required = set .intersection (* branch_required ) if branch_required else set ()
429+ required = sorted (top_required | common_required )
430+ if required :
431+ schema ["required" ] = required
432+ else :
433+ schema .pop ("required" , None )
434+ del schema [branch_key ]
435+ print (f" flattened root { branch_key } object overlay in { schema_path } " )
436+ return schema
437+
438+
335439def flatten_schemas (temp_dir : Path ):
336440 """
337441 Copy schemas to temp directory, preserving directory structure.
@@ -354,7 +458,11 @@ def flatten_schemas(temp_dir: Path):
354458 temp_dir .mkdir ()
355459
356460 # Recursively find all JSON schemas (including subdirectories)
357- schema_files = list (SCHEMAS_DIR .rglob ("*.json" ))
461+ # The generator assigns numeric suffixes while traversing this aggregate
462+ # input tree. Path.rglob() follows filesystem insertion order, which
463+ # differs between developer machines and fresh CI checkouts. Create the
464+ # temporary tree in a stable order so generated names are reproducible.
465+ schema_files = sorted (SCHEMAS_DIR .rglob ("*.json" ))
358466 # Skip the top-level index.json
359467 schema_files = [f for f in schema_files if f .name != "index.json" ]
360468 schema_files = [
@@ -394,12 +502,14 @@ def flatten_schemas(temp_dir: Path):
394502 # generated convenience model omits this field.
395503 properties .pop ("formats" , None )
396504
397- # Rewrite $ref paths: convert absolute paths to relative, hyphens to underscores
505+ # Normalize generator-specific extensions, then rewrite $ref paths.
506+ schema = normalize_enum_descriptions (schema )
398507 schema = rewrite_refs (schema , rel_path )
399508 schema = stabilize_inlined_core_refs (schema , rel_path )
400509 schema = stabilize_nested_discriminators (schema , rel_path )
401510
402- # Flatten validation-only anyOf/oneOf into single-class schemas
511+ # Flatten validation-only anyOf/oneOf into single-class schemas.
512+ schema = flatten_root_object_validation_union (schema , rel_path )
403513 schema = flatten_validation_oneof (schema )
404514
405515 with open (output_file , "w" ) as f :
@@ -568,6 +678,7 @@ def generate_root_discovery_types(input_dir: Path, output_dir: Path = OUTPUT_DIR
568678 source = SCHEMAS_DIR / schema_rel_path
569679 prepared = input_dir / f"_{ schema_rel_path .stem } _discovery.json"
570680 schema = json .loads (source .read_text ())
681+ schema = normalize_enum_descriptions (schema )
571682 schema = rewrite_refs (schema , schema_rel_path )
572683 schema = flatten_validation_oneof (schema )
573684 prepared .write_text (json .dumps (schema , indent = 2 ))
0 commit comments