fix: resolve a $ref through an import alias in a map - #120
Merged
Merged
Conversation
resolveMap had no tests. The tests cover primitive, pointer, any, struct, cross-package struct, slice and nested map value types. Each test also makes sure that prog.References defines every $ref that the schema gives. A reference that nothing defines makes an unusable document.
lookupTypeAndRef built the reference from the selector as written in the Go source. For a type that the file reaches through an import alias, that is the alias, but GetReference keeps the definition under the package name. The reference then pointed at a definition that does not exist. The reference name and the GetReference lookup now come from the import path that findType resolves.
The tests now cover map[string][]aliased.Nested, which goes through resolveArray. lookupTypeAndRef no longer gives a JSON Schema type, because no caller uses it.
A scratch probe for map[string]b.StatusType reached the tree by accident. The case fails, because resolveMap gives a reference for a named primitive.
Two packages that share a base name give one definition the base name and the other a numbered name. resolveMap built the name on its own, so both maps pointed at the first definition. An alias goes with this case, because a name clash is a reason to write one. The reference now comes from what GetReference stored. The name that lookupTypeAndRef gives stays for the error path.
Coverage Report for CI Build 36029501958Coverage increased (+2.0%) to 59.71%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
rafaeljusto
reviewed
Sep 24, 2026
resolveMap now sets additionalProperties only after GetReference succeeds, so a failed lookup no longer leaves a $ref that points at nothing.
resolveMap sent a named slice such as bars to GetReference. That gave a $ref to one element, and for a package on a dotted import path it gave a $ref to nothing. It now resolves the element through resolveArray, the same as fieldToSchema does.
resolveMap sent the full import path to GetReference. GetReference stores a definition by base name, so the full lookup never found it. It parsed the type again and replaced its context. resolveMap now checks for the stored key first.
Contributor
Author
|
A local review of this branch raised these points.
|
rafaeljusto
approved these changes
Sep 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A map value type that the Go source reaches through an import alias got a
$refunder the alias. Kommentaar keeps the definition under the package name, so the reference pointed at nothing, and onlydbgreported it.Changes
resolveMapnames the$refafter the definition thatGetReferencestored. An import alias or a base name clash no longer gives a reference to nothing or to the wrong type.map[string]barsgives an array of its element, the same asfieldToSchemadoes.resolveMapreuses a stored definition before it callsGetReference.GetReferencecannot resolve, such asmap[string]pkg.StatusType, gives an open object, not a dangling$ref.resolveMapstill sends theGetReferenceerror todbgalone. A separate change makes it a failure.resolveArrayandfieldToSchemastill name the$refby base name.resolveMaptests assert that every$refin the schema has a definition inprog.References.Focus areas
find.go:GetReferencetakes its key from the newreferenceLookup. The key rule did not change.jsonschema.go:resolveMapcallsreferenceLookupfirst, becauseGetReferencedoes not find a stored definition by its full import path. Without the check, a self-referencing map value recursed until the stack overflowed.Files changed in
docparse/find.goreferenceLookupjsonschema.gojsonschema_test.goresolveMaptestdata/src/a/a.gotestdata/src/c/c.gotestdata/src/d/c/c.gotestdata/src/example.com/m/m.goScreenshots