Skip to content

Commit f383dc4

Browse files
build(deps-dev): bump datamodel-code-generator to 0.63.0 (#1130)
* build(deps-dev): bump datamodel-code-generator Bumps the pip group with 1 update in the / directory: [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator). Updates `datamodel-code-generator` from 0.56.1 to 0.64.0 - [Release notes](https://github.com/koxudaxi/datamodel-code-generator/releases) - [Changelog](https://github.com/koxudaxi/datamodel-code-generator/blob/main/CHANGELOG.md) - [Commits](datamodel-code-generator/datamodel-code-generator@0.56.1...0.64.0) --- updated-dependencies: - dependency-name: datamodel-code-generator dependency-version: 0.64.0 dependency-type: direct:development dependency-group: pip ... Signed-off-by: dependabot[bot] <support@github.com> * fix(codegen): support secure generator upgrade * fix(codegen): stabilize generated principal aliases * fix(codegen): keep collision snapshot release-aligned * fix(codegen): disambiguate generated response enum --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Brian O'Kelley <bokelley@scope3.com>
1 parent 813cf2a commit f383dc4

9 files changed

Lines changed: 532 additions & 39 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ jobs:
372372
run: python scripts/bundle_schemas.py
373373

374374
- name: Generate models
375-
if: steps.version-check.outputs.is_prerelease != 'true'
376375
run: python scripts/generate_types.py
377376

378377
- name: Validate generated code syntax

‎pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ dev = [
125125
# Pin to exact version: codegen's variant numbering (e.g. CreateMediaBuyResponse1 vs
126126
# CreateMediaBuyResponse) shifts between versions, producing diff churn and breaking
127127
# generated-code imports that reference specific suffixes.
128-
"datamodel-code-generator[http]==0.56.1",
128+
"datamodel-code-generator[http]==0.63.0",
129129
# Runs Starlette app lifespan under httpx.ASGITransport in tests —
130130
# the canonical library for what httpx doesn't do natively. Used by
131131
# tests/test_mcp_middleware_composition.py and future integration
@@ -340,7 +340,7 @@ skips = ["B101"] # Allow assert in code (we're not using -O optimization)
340340

341341
[dependency-groups]
342342
dev = [
343-
"datamodel-code-generator==0.56.1",
343+
"datamodel-code-generator==0.63.0",
344344
"pre-commit>=4.4.0",
345345
"types-protobuf>=7.34.1.20260408",
346346
# Pinned in the dev group so ``uv run mypy`` (used by the pre-commit

‎scripts/generate_ergonomic_coercion.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,10 @@ def _find_media_buy_success_variant(module: Any) -> type[_PydBaseModel] | None:
419419
lines.append(f"from adcp.types.generated_poc.{path} import {name}")
420420

421421
# Add request type imports
422-
lines.append("from adcp.types.generated_poc.media_buy.create_media_buy_request import (")
423-
lines.append(" CreateMediaBuyRequest,")
424-
lines.append(")")
422+
if "CreateMediaBuyRequest" in all_coercions:
423+
lines.append("from adcp.types.generated_poc.media_buy.create_media_buy_request import (")
424+
lines.append(" CreateMediaBuyRequest,")
425+
lines.append(")")
425426
lines.append("from adcp.types.generated_poc.media_buy.get_products_request import (")
426427
lines.append(" GetProductsRequest,")
427428
lines.append(")")

‎scripts/generate_types.py‎

Lines changed: 116 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
154177
def 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+
335439
def 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

Comments
 (0)