Problem Description
When a nested array type (e.g., [][]interface{}) is defined in an .api file, the Swagger 2.0 specification generated by goctl lacks the required items field in the array schema, violating the Swagger 2.0 specification.
Reproduction Steps
Create nested-array.api:
syntax = "v1"
type TestResponse {
Data [][]interface{} `json:"data"`
}
@server (
prefix: /api
)
service test-api {
@handler GetTest
get /test returns (TestResponse)
}
Method 1: Using plugin (old way)
goctl api plugin -plugin goctl-swagger="swagger -filename nested-array.json" -api nested-array.api -dir .
Method 2: Using built-in command (recommended way)
goctl api swagger -filename nested-array.json -api nested-array.api -dir .
Actual Result
Both methods produce the same incorrect output. The generated nested-array.json contains:
{
"definitions": {
"TestResponse": {
"type": "object",
"properties": {
"data": {
"type": "array"
}
}
}
}
}
Note: The schema for the data field only has "type": "array", missing the items field.
Expected Result
According to the Swagger 2.0 specification, array types must include the items field:
{
"definitions": {
"TestResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "object"
}
}
}
}
}
}
}
Specification Requirements
Swagger 2.0 Specification (Schema Object):
If type is "array", the items property is required.
OpenAPI 3.0 Specification (Schema Object):
Value MUST be an object and not an array. Inline or referenced schema MUST be of a Schema Object and not a Standard JSON Schema. items MUST be present if the type is array.
Impact
- Generated Swagger documentation cannot pass strict schema validation (e.g.,
kin-openapi's Validate())
- Toolchain (such as Swagger UI, OpenAPI generators) may not parse or render API documentation correctly
- Client code generators may produce incorrect type definitions
Technical Analysis
Related Files:
tools/goctl/api/swagger/properties.go - Handles type to Swagger property conversion
tools/goctl/api/swagger/definition.go - Generates definitions
tools/goctl/api/swagger/response.go - Generates response schemas
Possible Causes:
- The type parser does not recursively generate the
items for inner arrays when processing nested arrays
- The
interface{} type mapping logic may be missing or incomplete
- The array type conversion code may be missing
items field generation
Suggested Fixes:
- Enhance array type handling logic in
properties.go
- Recursively process nested arrays, generating corresponding
items schemas for each layer
- For
interface{} type, at minimum generate {"type": "object"} as items
- Add unit tests to cover nested array scenarios
Real-World Case
In the stability-alert-center project, the following type definition:
type AlertConfig struct {
// ...
NotifyTarget [][]interface{} `json:"notifyTarget"`
// ...
}
The generated Swagger has multiple instances of missing items, causing OpenAPI validation failures.
Problem Description
When a nested array type (e.g.,
[][]interface{}) is defined in an.apifile, the Swagger 2.0 specification generated by goctl lacks the requireditemsfield in the array schema, violating the Swagger 2.0 specification.Reproduction Steps
Create
nested-array.api:Method 1: Using plugin (old way)
Method 2: Using built-in command (recommended way)
goctl api swagger -filename nested-array.json -api nested-array.api -dir .Actual Result
Both methods produce the same incorrect output. The generated
nested-array.jsoncontains:{ "definitions": { "TestResponse": { "type": "object", "properties": { "data": { "type": "array" } } } } }Note: The schema for the
datafield only has"type": "array", missing theitemsfield.Expected Result
According to the Swagger 2.0 specification, array types must include the
itemsfield:{ "definitions": { "TestResponse": { "type": "object", "properties": { "data": { "type": "array", "items": { "type": "array", "items": { "type": "object" } } } } } } }Specification Requirements
Swagger 2.0 Specification (Schema Object):
OpenAPI 3.0 Specification (Schema Object):
Impact
kin-openapi'sValidate())Technical Analysis
Related Files:
tools/goctl/api/swagger/properties.go- Handles type to Swagger property conversiontools/goctl/api/swagger/definition.go- Generates definitionstools/goctl/api/swagger/response.go- Generates response schemasPossible Causes:
itemsfor inner arrays when processing nested arraysinterface{}type mapping logic may be missing or incompleteitemsfield generationSuggested Fixes:
properties.goitemsschemas for each layerinterface{}type, at minimum generate{"type": "object"}as itemsReal-World Case
In the
stability-alert-centerproject, the following type definition:The generated Swagger has multiple instances of missing
items, causing OpenAPI validation failures.