mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 00:40:00 +08:00
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:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user