Skip to content

Add API/export contract enforcement checks#24

Merged
ProfRandom92 merged 1 commit into
mainfrom
codex/add-api/export-contract-enforcement-checks
May 11, 2026
Merged

Add API/export contract enforcement checks#24
ProfRandom92 merged 1 commit into
mainfrom
codex/add-api/export-contract-enforcement-checks

Conversation

@ProfRandom92

Copy link
Copy Markdown
Owner

Motivation

  • Enforce API/dashboard/export payload shapes against the machine-readable contracts under contracts/ to catch structural regressions early.
  • Use deterministic, synthetic fixtures so validation requires no live server and includes no real Daimler data or secrets.
  • Integrate these checks into the agent CI guardrail so pull requests validate generated fixtures and contract conformance automatically and the PR targets main (links issue Add API/export contract enforcement checks #23).

Description

  • Add scripts/generate_contract_fixtures.py which emits a deterministic synthetic fixture at contracts/examples/api-dashboard.example.json (with generated_at: "2026-01-01T00:00:00Z") and writes docs/reports/contract-fixture-generation-report.md.
  • Add scripts/validate_api_exports.py which loads contracts/api-dashboard.schema.json and the generated example and performs lightweight standard-library checks for required fields, simple types, synthetic: true, and that api_routes, dashboard_views, export_formats, and report_integration_points are arrays, then writes docs/reports/api-export-validation-report.md.
  • Update .github/workflows/agent-checks.yml to py_compile the new scripts and to run python scripts/generate_contract_fixtures.py and python scripts/validate_api_exports.py during CI.
  • Update docs/API_SURFACE.md and docs/AGENT_WORKFLOW.md to document the new scripts, the example contracts/examples/api-dashboard.example.json, and the generated reports in docs/reports/.

Testing

  • Compiled scripts with python -m py_compile scripts/generate_contract_fixtures.py scripts/validate_api_exports.py (passed).
  • Ran python scripts/generate_contract_fixtures.py to generate contracts/examples/api-dashboard.example.json and docs/reports/contract-fixture-generation-report.md (passed).
  • Ran python scripts/validate_api_exports.py to validate the generated fixture against contracts/api-dashboard.schema.json and produce docs/reports/api-export-validation-report.md (passed).
  • Ran python scripts/validate_contracts.py to refresh the contract validation report including the new example (passed).

Codex Task

@ProfRandom92 ProfRandom92 merged commit f4543c3 into main May 11, 2026
1 of 2 checks passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new synthetic API/dashboard/export contract fixture along with automated generation and validation scripts. Key additions include scripts/generate_contract_fixtures.py and scripts/validate_api_exports.py, which are integrated into the agent workflow and CI guardrails to ensure deterministic, privacy-compliant validation without requiring a live server. Feedback focuses on significant logic duplication between the new validation script and existing tools, missing support for numeric types in the validator, and redundant manual checks for array fields that are already covered by schema-based validation.

Comment on lines +1 to +7
#!/usr/bin/env python3
"""Validate synthetic API/dashboard/export fixtures against the contract schema.

This is a lightweight, deterministic validator for the generated fixture. It uses
only the Python standard library and performs a small subset of JSON Schema-like
checks that match the current machine-readable contract needs.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This script introduces significant logic duplication with scripts/validate_contracts.py (e.g., JSON loading, type checking, and schema validation loops). The only unique validation performed here is the enforcement of the synthetic: true flag. To improve maintainability and ensure a single source of truth for contract validation, consider integrating the synthetic check into scripts/validate_contracts.py and removing this redundant script.

Comment on lines +20 to +21
SUPPORTED_TYPES = {"string", "boolean", "object", "array"}
ARRAY_FIELDS = {"api_routes", "dashboard_views", "export_formats", "report_integration_points"}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The SUPPORTED_TYPES set is missing number and integer, which are supported in scripts/validate_contracts.py. This inconsistency makes the validator fragile if the schema is updated to include numeric fields. Additionally, the ARRAY_FIELDS set and its associated manual check (lines 110-112) are redundant because the schema-based validation loop (lines 97-105) already verifies that these fields match the array type defined in the schema. Note that ARRAY_FIELDS also currently omits security_notes.

Suggested change
SUPPORTED_TYPES = {"string", "boolean", "object", "array"}
ARRAY_FIELDS = {"api_routes", "dashboard_views", "export_formats", "report_integration_points"}
SUPPORTED_TYPES = {"string", "number", "integer", "boolean", "object", "array"}

Comment on lines +33 to +56
def type_name(value: Any) -> str:
if isinstance(value, bool):
return "boolean"
if isinstance(value, str):
return "string"
if isinstance(value, dict):
return "object"
if isinstance(value, list):
return "array"
if value is None:
return "null"
return type(value).__name__


def matches_type(value: Any, expected: str) -> bool:
if expected == "string":
return isinstance(value, str)
if expected == "boolean":
return isinstance(value, bool)
if expected == "object":
return isinstance(value, dict)
if expected == "array":
return isinstance(value, list)
return False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These utility functions are duplicated from scripts/validate_contracts.py but lack support for numeric types. If this script is retained, these functions should be updated to handle int and float (mapping to integer and number) to maintain consistency across the repository's validation tools.

def type_name(value: Any) -> str:
    if isinstance(value, bool):
        return "boolean"
    if isinstance(value, str):
        return "string"
    if isinstance(value, int):
        return "integer"
    if isinstance(value, float):
        return "number"
    if isinstance(value, dict):
        return "object"
    if isinstance(value, list):
        return "array"
    if value is None:
        return "null"
    return type(value).__name__


def matches_type(value: Any, expected: str) -> bool:
    if expected == "string":
        return isinstance(value, str)
    if expected == "number":
        return isinstance(value, (int, float)) and not isinstance(value, bool)
    if expected == "integer":
        return isinstance(value, int) and not isinstance(value, bool)
    if expected == "boolean":
        return isinstance(value, bool)
    if expected == "object":
        return isinstance(value, dict)
    if expected == "array":
        return isinstance(value, list)
    return False

Comment on lines +110 to +112
for field in sorted(ARRAY_FIELDS):
if field in fixture and not isinstance(fixture[field], list):
errors.append(f"fixture field {field} must be an array")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

As noted previously, this manual check for array fields is redundant. The validation loop at lines 97-105 already checks the fixture fields against the types specified in contracts/api-dashboard.schema.json. Since the schema defines these fields as array, they are already being verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant