Skip to content

fix(goctl/swagger): populate required fields in top-level response sc… - #5663

Open
OSHMKUFA5100 wants to merge 1 commit into
zeromicro:masterfrom
OSHMKUFA5100:master
Open

fix(goctl/swagger): populate required fields in top-level response sc…#5663
OSHMKUFA5100 wants to merge 1 commit into
zeromicro:masterfrom
OSHMKUFA5100:master

Conversation

@OSHMKUFA5100

Copy link
Copy Markdown

Problem

goctl api swagger generates response schemas where the top-level object is always missing the required array, even when struct fields are non-pointer and not marked optional. 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

propertiesFromType returns two values: (properties, requiredFields). Every other call site correctly assigns requiredFields to schema.Required:

Call site Handles requiredFields?
definition.go (definitions section)
swagger.go (array items via itemFromGoType)
properties.go (nested objects)
response.go (top-level response) ❌ discarded with _

In response.go:

p, _ := propertiesFromType(ctx, tp)   // requiredFields discarded
props.Type = typeFromGoType(ctx, tp)
props.Properties = p
// props.Required never set

This is why nested objects (e.g. array items) correctly emit required, but the top-level response object never does.

Fix

Capture the requiredFields return value and assign it to props.Required:

p, r := propertiesFromType(ctx, tp)
props.Type = typeFromGoType(ctx, tp)
props.Properties = p
props.Required = r

Test

Added TestResponseRequiredFields covering:

  • Fields without optional tag → appear in required array
  • Field with optional tag → excluded from required array, still present in properties

All existing tests pass, no regressions.

Closes #4955

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant