Skip to content

fix(mock): match {param} path templates and resolve nested $ref/anyOf/oneOf/allOf - #29

Merged
vaskoevgen merged 10 commits into
mainfrom
yevhen/baton-mock-path-templates-refs
Nov 17, 2026
Merged

vaskoevgen merged 10 commits into
mainfrom
yevhen/baton-mock-path-templates-refs

Conversation

@vaskoevgen

@vaskoevgen vaskoevgen commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Why

infra-baton's staging Baton can't serve the current Wander service specs. Tested with the current @wandercom/*-client specs on Baton 0.4.2:

  • Paths with {param} never match. MockServer looks routes up by exact text, so GET /invoices/123 → 404 for /invoices/{id}. This already affects the specs in use today: 32 of 46 payment paths and 30 of 39 property paths are templated. Search moved from literal /collections/wander/... to /collections/{collectionId}/..., so its mock started returning 404 too.
  • Nested $ref and composite schemas produce {}. generate_instance didn't follow $ref below the top of the response, or #/components/responses/..., and didn't handle anyOf/oneOf/allOf/const. Examples: rate_cents: {} instead of an integer, and data: [{}] instead of objects with their required fields.

What changed (src/baton/mock.py only)

  • Path templates: add_routes compiles {param} paths once. Lookup order: exact literal path, then a literal path differing only by a trailing slash, then templates, most specific first (more literal segments). A trailing slash is optional on both the spec path and the request, for literal and templated paths alike. Each placeholder matches exactly one non-empty segment. Query stripping, the 404 body and the method check are unchanged.
  • Schema generation: local $ref is resolved at any depth (schemas and responses, JSON Pointer ~0/~1). anyOf/oneOf produce a value from one branch, preferring non-null. allOf merges properties, required fields and bounds, intersects enum/const, and keeps both compositions when a $ref and its siblings each carry allOf. const and OpenAPI 3.1 type lists are supported. example/default/enum keep their precedence.
  • Recursion: a recursive $ref is generated once as a real instance, then cut off at the next repetition; a depth guard of 32 also applies. Output stays finite and fast, including a required array of self-references.
  • Unchanged interface: load_routes()/parse_openapi() still return {spec_path: {METHOD: body}}, which collapse.py relies on. generate_instance(schema) still works without the new optional components argument. No new dependencies.

Tests (tests/test_mock.py; existing tests unchanged)

New TestPathTemplates and TestSchemaComposition cover R1–R11: template matching, literal and most-specific precedence, 404 cases, nested and response $ref, required/self/mutual recursion in bounded time, anyOf/oneOf/allOf/const, and unchanged example/default/enum precedence. They exercise real MockServer sockets and load_routes.

Verification

  • Full suite: 1223 passed, 0 failed.
  • Negative control: the new tests (19 initial and 3 recursion) fail on main (a7266a7) and pass with this change.
  • Real specs, run locally with infra-baton's tests/smoke.py:
    • with today's infra-baton specs: 18/20, identical to 0.4.0, with the same route counts (no regression);
    • with the current client specs: 17/20, up from 14/20, with the search, pricing and property checks fixed.
    • The remaining failures are environment-only (no local wander api-server, local slot test), plus one infra-baton smoke.py literal-path lookup that will be fixed in the spec refresh PR.

Known, accepted differences

  • Template lookup is method-blind: the most specific matching path is chosen first, and a missing method there returns 404 without trying a less specific template. In the current specs this only affects unrealistic requests (for example GET /invoices/preview, where /invoices/preview is POST-only).

  • Because a trailing slash is optional, /x/{id} and /x/{id}/ compile to the same template. If a spec contained both with different methods, only the first one's methods would be reachable. None of the 6 mocked Wander specs has any path ending in /, so this can't occur today. A follow-up could merge method tables for identical templates.

  • The template loop in _handle still checks pattern.fullmatch(path) before alt_path. Since compiled templates never end with /, that first check is redundant. Cosmetic, no behavior impact.

  • allOf enum/const intersection uses Python equality, so true and 1 compare equal (e.g. enum: [1, 2] with const: true yields 1). No current Wander spec mixes booleans and integers in an allOf.

After merge

Cut a release tag (for example v0.4.3) so infra-baton can bump Baton and refresh its specs.

How it was built

This PR was built by the Software Factory: Codex (gpt-6.1-sol) wrote the implementation, Cursor (grok-4.7-high) wrote the tests independently without seeing it, and Claude (validator) reviewed them, with agy (gemini-4-carbon-high) providing oversight. Repair rounds fixed recursive references being cut off too early and addressed every review finding ($ref sibling merging, no synthetic map keys, per-load log de-duplication, trailing-slash spec paths, test docstrings) and two independent review findings ($ref + sibling allOf dropping required fields; overlapping enums in allOf).

PR built by Factory v0.8.29
FactoryRun: baton-mock-templates-20261117

🤖 Generated with Claude Code

…/oneOf/allOf/const

- MockServer matches requests against templated spec paths after literal
  paths; the most specific template wins; trailing slash and query handling kept.
- generate_instance resolves local $ref at any depth (schemas and responses),
  handles anyOf/oneOf (non-null preferred), allOf (merged), const and 3.1 type
  lists, with a cycle/depth guard.
- load_routes/parse_openapi keep the {spec_path: {METHOD: body}} shape.

Implementation authored by the Factory Coder lane (codex gpt-6.1-sol).

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
Covers R1-R11: templated paths, literal/specificity precedence, 404 cases,
nested and response $ref, recursion bounds, anyOf/oneOf/allOf/const, and
unchanged example/default/enum precedence. Existing tests unchanged.

Tests authored independently by the Factory Tester lane (cursor-agent grok-4.7-high).

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
A required self-reference returned {} at the first repetition, so e.g.
Node.child or Tree.kids were empty objects. Emit one full instance of a
directly self-referencing schema and cut only its next link; mutual
recursion and the depth guard are unchanged.

Implementation authored by the Factory Coder lane (codex gpt-6.1-sol), repair round 1.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
Required self-reference, required array of self-references (minItems 2)
and required mutual recursion must load and serve in bounded time with the
first followed instance generated.

Tests authored independently by the Factory Tester lane (cursor-agent grok-4.7-high), repair round 1.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
@vaskoevgen vaskoevgen added the Factory PR opened by the Factory label Nov 17, 2026

@adaptcom adaptcom Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Confidence Score: 4/5

Summary

Latest head adds allOf enum/const intersection and keeps $ref-plus-sibling allOf compositions; prior findings are resolved or explicitly accepted. 1223 tests pass, real infra-baton specs load with far fewer empty leaves. Low risk.

Important Files Changed

File Overview
src/baton/mock.py _merge_schema now concatenates allOf and intersects enum/const; diagnostics set threaded per load
tests/test_mock.py Three new allOf tests; fail on previous head, pass here

Findings

  • src/baton/mock.py:65 — Membership uses Python equality, so enum [1,2] intersected with const true yields 1; compare with matching JSON types (exclude bool/int cross-matches) if exactness matters.

Reviewed a7266a7...cba4d13 · ↻ Re-run review · View in Adapt

Comment thread src/baton/mock.py Resolved
Comment thread src/baton/mock.py Resolved
Comment thread src/baton/mock.py Resolved
Comment thread src/baton/mock.py Resolved
Comment thread src/baton/mock.py Resolved
Comment thread tests/test_mock.py Resolved
Comment thread tests/test_mock.py Resolved
- $ref sibling keys are merged with the referenced schema (_merge_schema)
  instead of replacing its properties/required.
- Maps (additionalProperties only) get entries only when minProperties
  requires them; otherwise {} as before.
- Single example/default/enum/const precedence check after expansion.
- Unresolved-ref log de-duplication is scoped to one spec load.

Implementation authored by the Factory Coder lane (codex gpt-6.1-sol), repair round 2.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
No assertion changes.

Tests authored independently by the Factory Tester lane (cursor-agent grok-4.7-high), repair round 2.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
A spec path ending in / (literal or templated) now matches the request with
or without the trailing slash. Exact literal matches still win first.

Implementation authored by the Factory Coder lane (codex gpt-6.1-sol), repair round 3.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
Tests authored independently by the Factory Tester lane (cursor-agent grok-4.7-high), repair round 3.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
@vaskoevgen

Copy link
Copy Markdown
Collaborator Author

@adaptcom review

- A $ref whose target and sibling both have allOf now keeps both lists, so
  the referenced schema's required fields are not dropped.
- allOf intersects enum/const values instead of overwriting them; an empty
  intersection falls back to the first part and is logged once at debug.

Implementation authored by the Factory Coder lane (codex gpt-6.1-sol), repair round 4 (independent review findings).

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
Tests authored independently by the Factory Tester lane (cursor-agent grok-4.7-high), repair round 4.

FactoryRun: baton-mock-templates-20261117
Built-By: Factory v0.8.29
@vaskoevgen

Copy link
Copy Markdown
Collaborator Author

@adaptcom review

@vaskoevgen
vaskoevgen merged commit e6e19f1 into main Nov 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Factory PR opened by the Factory

Development

Successfully merging this pull request may close these issues.

1 participant