fix(goctl/swagger): populate required fields in top-level response sc… - #5663
Open
OSHMKUFA5100 wants to merge 1 commit into
Open
fix(goctl/swagger): populate required fields in top-level response sc…#5663OSHMKUFA5100 wants to merge 1 commit into
OSHMKUFA5100 wants to merge 1 commit into
Conversation
…hema propertiesFromType returns both properties and requiredFields, but jsonResponseFromType discarded the requiredFields with _, causing top-level response schemas to always have an empty required array. This made all response fields appear as optional in tools like Apifox. Closes zeromicro#4955
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.
Problem
goctl api swaggergenerates response schemas where the top-level object is always missing therequiredarray, even when struct fields are non-pointer and not markedoptional. This causes all response fields to appear as optional in tools like Apifox / Swagger UI.Reported in #4955 (opened Jun 2025, stale, no assignee, no linked PR).
Root Cause
propertiesFromTypereturns two values:(properties, requiredFields). Every other call site correctly assignsrequiredFieldstoschema.Required:requiredFields?definition.go(definitions section)swagger.go(array items viaitemFromGoType)properties.go(nested objects)response.go(top-level response)_In
response.go:This is why nested objects (e.g. array items) correctly emit
required, but the top-level response object never does.Fix
Capture the
requiredFieldsreturn value and assign it toprops.Required:Test
Added
TestResponseRequiredFieldscovering:optionaltag → appear inrequiredarrayoptionaltag → excluded fromrequiredarray, still present inpropertiesAll existing tests pass, no regressions.
Closes #4955