ISSUE DESCRIPTION Generator: emit resolveBaseURL when URL fields contain placeholders…
- User
ISSUE DESCRIPTION
Generator: emit resolveBaseURL when URL fields contain placeholders
When an API specification contains
{PLACEHOLDER}tokens in URL-bearing fields, the generator must infer those placeholder names intoEndpointTemplateVarsbefore validation and generation so the generated client includesresolveBaseURLsupport.Inspect
BaseURL,BasePath,GraphQLEndpointPath, tier base URLs, and resource, endpoint, and subresource base URLs. Preserve existing explicit entries and their order, append each newly discovered placeholder once, and use deterministic ordering when traversing maps. Generation from a templated base URL must produce a buildable client containing the URL resolver.Evaluation contract
Generator.Newmust invoke placeholder inference before it returns, so callers can observe the inferredEndpointTemplateVarsimmediately and later validation/generation uses the same populated specification. Scan every documented URL-bearing field, preserve explicit variables and their order, append each inferred name once, and make map-backed traversal deterministic.Interface
Type: Function Name: InferEndpointTemplateVarsFromBaseURLs Path: internal/spec/spec.go Input: (no parameters) Output: (no return value; mutates receiver's EndpointTemplateVars) Description: Scans the receiver's BaseURL, BasePath, GraphQLEndpointPath, tier BaseURLs, and resource/endpoint/subresource BaseURLs for
{PLACEHOLDER}tokens and appends any newly discovered placeholders to EndpointTemplateVars, preserving existing explicit entries and their order. Call after loading or modifying any URL-bearing field but before validation or code generation.ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
Assistant
Now let's look at Resource/Endpoint structs and existing placeholder extraction logic (there might already be a regex used elsewhere).
Assistant
Good, there's an existing
pathParamRe. Let's check Resource and Endpoint struct definitions for BaseURL fields, and how Resources map with subresources are structured (map[string]Resource, with sub-resources nested).Assistant
Now let's check for
resolveBaseURLin templates/generator to understand where it's triggered.Assistant
Now let's add the call before
return g.Assistant
Let's use edit with line range directly.
Assistant
Now let's implement
InferEndpointTemplateVarsFromBaseURLsin spec.go. Let's find a good place, near HasResourceBaseURLOverride or near EndpointTemplateVars definitions.Assistant
Not imported. Need to add it. Now let's find
HasResourceBaseURLOverridelocation to place new function nearby (around line 1330-1360).Assistant
Now let's write the new function. I'll insert it right after
resourceHasBaseURLOverridefunction (line 1359, beforetype Endpoint struct).Assistant
Now insert new function after line 1360.
Assistant
Now add
sortimport.Assistant
Now let's build.
Assistant
Build succeeds. Now let's run existing tests related to spec and generator.
Assistant
Confirmed pre-existing failure unrelated to our change (Go toolchain vulnerability check). Let's run full generator test suite skipping that specific test to check our changes don't break anything else.
Assistant
All these tests pass. Let's check that byte-compat tests (e.g. TestGenerateNoEndpointTemplateVarsByteCompat) still pass, especially given our new inference could alter output for existing test specs. They passed. Now let's write a quick reproduction script to verify the new function and Generator.New behavior.
Assistant
Works as expected: preserves order, deduplicates, traverses resources. Now let's test with pre-existing explicit vars preserved, and also GraphQLEndpointPath, TierRouting, sub-resources, and endpoint-level BaseURL.
Assistant
Excellent — order preserved for explicit entries, deduplication works, deterministic map ordering (a-tier before z-tier alphabetically, ares before zres), idempotent across repeated calls. Now clean up temp files.
Assistant
Now let's check if there's an existing test that references
InferEndpointTemplateVarsFromBaseURLsor similar, or existing url.go.tmpl test around building a client with a templated base URL — to make sure our change makes the "Generation from a templated base URL must produce a buildable client" contract true.Assistant
Now let's build the generated CLI to verify it's buildable.
Assistant
url.go is present, confirming resolveBaseURL/buildURL support was emitted. Now let's build it.
Assistant
The generated client builds successfully. Clean up temp files/directories.
Assistant
Now let's run gofmt and vet on the modified files, plus run full spec and generator test suites once more (excluding the known unrelated govulncheck flake) to be thorough.
Assistant
Clean. Now run the whole test suite for spec and generator packages, skipping the known flaky govulncheck test.
Assistant
Only the pre-existing unrelated govulncheck failure remains (confirmed earlier to fail identically on the unmodified repo). Let's also run the broader test suite across the repo to make sure nothing else broke due to the new inference being invoked in
New.Assistant
Let's verify this test is pre-existing broken (unrelated to our change) by checking on the stashed original code.
Assistant
Confirmed pre-existing failure unrelated to our changes (environmental, likely filesystem walk issue). Both failing tests (
TestGenerateWithNoAuthgovulncheck andTestLiveCheck_BinaryRefreshReasonIncludesSourceWalkError) fail identically before our change. Our modifications don't introduce any new failures.Assistant
The diff looks clean and complete. Let's do a final full test run confirmation summary.
Assistant
Now let's go through the checklist explicitly.
Checklist review:
-
If an error was reported in the issue description, I have successfully reproduced it. The issue wasn't a bug report with an error message but a feature request (implement
InferEndpointTemplateVarsFromBaseURLsand wire it intoGenerator.New). I "reproduced" the gap by writing a script showing a spec withBaseURL: "https://{shop}.example.com"produced noEndpointTemplateVarsand nourl.go(noresolveBaseURL/buildURLsupport) prior to my change — confirmed via inspectingrenderOptionalSupportFilesgating onlen(g.Spec.EndpointTemplateVars) > 0. -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. After implementing the function and wiring it into
Generator.New, I reran the same script:EndpointTemplateVarswas correctly inferred ([shop]),url.gowas generated, and the full generated CLI built successfully withgo build ./....
-
Assistant
-
I have thought about edge cases and made sure my fix handles them as well. Covered: nil receiver guard; preserving explicit
EndpointTemplateVarsentries and their order; deduplication across all fields (an explicit var and an inferred var with the same name aren't duplicated, and the same placeholder appearing in multiple fields is appended only once); deterministic ordering via sorted map-key traversal forResources,Endpoints,SubResources, andTierRouting.Tiers; recursive handling of arbitrarily nestedSubResources; idempotency (calling the function twice produces the same result, verified in my test script); empty/no-placeholder URLs are simply no-ops (regex finds nothing). -
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. Ran
internal/specandinternal/generatortest suites in full, plus the entire repo test suite. All pass except two pre-existing, unrelated failures (TestGenerateWithNoAuth'sgovulncheckgate failing due to a stdlib CVE in the sandboxed Go toolchain, and ), both of which I verified fail identically on the unmodified repository via .
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.