fix(goctl/swagger): correct $ref placement in array definitions when useDefinitions is enabled (#5199)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: kevwan <1918356+kevwan@users.noreply.github.com>
This commit is contained in:
Copilot
2025-10-02 22:11:18 +08:00
committed by GitHub
parent fa85c84af3
commit ce6d0e3ea7
2 changed files with 145 additions and 5 deletions

View File

@@ -70,15 +70,40 @@ func propertiesFromType(ctx Context, tp apiSpec.Type) (spec.SchemaProperties, []
switch sampleTypeFromGoType(ctx, member.Type) {
case swaggerTypeArray:
schema.Items = itemsFromGoType(ctx, member.Type)
// Special handling for arrays with useDefinitions
if ctx.UseDefinitions {
// For arrays, check if the array element (not the array itself) contains a struct
if arrayType, ok := member.Type.(apiSpec.ArrayType); ok {
if structName, containsStruct := containsStruct(arrayType.Value); containsStruct {
// Set the $ref inside the items, not at the schema level
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef(getRefName(structName)),
},
},
}
}
}
}
case swaggerTypeObject:
p, r := propertiesFromType(ctx, member.Type)
schema.Properties = p
schema.Required = r
}
if ctx.UseDefinitions {
structName, containsStruct := containsStruct(member.Type)
if containsStruct {
schema.SchemaProps.Ref = spec.MustCreateRef(getRefName(structName))
// For objects with useDefinitions, set $ref at schema level
if ctx.UseDefinitions {
structName, containsStruct := containsStruct(member.Type)
if containsStruct {
schema.SchemaProps.Ref = spec.MustCreateRef(getRefName(structName))
}
}
default:
// For non-array, non-object types, apply useDefinitions logic
if ctx.UseDefinitions {
structName, containsStruct := containsStruct(member.Type)
if containsStruct {
schema.SchemaProps.Ref = spec.MustCreateRef(getRefName(structName))
}
}
}