(t *testing.T)
| 26 | } |
| 27 | |
| 28 | func TestArrayDefinitionsBug(t *testing.T) { |
| 29 | // Test case for the bug where array of structs with useDefinitions |
| 30 | // generates incorrect swagger JSON structure |
| 31 | |
| 32 | // Context with useDefinitions enabled |
| 33 | ctx := Context{ |
| 34 | UseDefinitions: true, |
| 35 | } |
| 36 | |
| 37 | // Create a test struct containing an array of structs |
| 38 | testStruct := spec.DefineStruct{ |
| 39 | RawName: "TestStruct", |
| 40 | Members: []spec.Member{ |
| 41 | { |
| 42 | Name: "ArrayField", |
| 43 | Type: spec.ArrayType{ |
| 44 | Value: spec.DefineStruct{ |
| 45 | RawName: "ItemStruct", |
| 46 | Members: []spec.Member{ |
| 47 | { |
| 48 | Name: "ItemName", |
| 49 | Type: spec.PrimitiveType{RawName: "string"}, |
| 50 | Tag: `json:"itemName"`, |
| 51 | }, |
| 52 | }, |
| 53 | }, |
| 54 | }, |
| 55 | Tag: `json:"arrayField"`, |
| 56 | }, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | // Get properties from the struct |
| 61 | properties, _ := propertiesFromType(ctx, testStruct) |
| 62 | |
| 63 | // Check that we have the array field |
| 64 | assert.Contains(t, properties, "arrayField") |
| 65 | arrayField := properties["arrayField"] |
| 66 | |
| 67 | // Verify the array field has correct structure |
| 68 | assert.Equal(t, "array", arrayField.Type[0]) |
| 69 | |
| 70 | // Check that we have items |
| 71 | assert.NotNil(t, arrayField.Items, "Array should have items defined") |
| 72 | assert.NotNil(t, arrayField.Items.Schema, "Array items should have schema") |
| 73 | |
| 74 | // The FIX: $ref should be inside items, not at schema level |
| 75 | hasRef := arrayField.Ref.String() != "" |
| 76 | assert.False(t, hasRef, "Schema level should NOT have $ref") |
| 77 | |
| 78 | // The $ref should be in the items |
| 79 | hasItemsRef := arrayField.Items.Schema.Ref.String() != "" |
| 80 | assert.True(t, hasItemsRef, "Items should have $ref") |
| 81 | assert.Equal(t, "#/definitions/ItemStruct", arrayField.Items.Schema.Ref.String()) |
| 82 | |
| 83 | // Verify there are no other properties in the items when using $ref |
| 84 | assert.Nil(t, arrayField.Items.Schema.Properties, "Items with $ref should not have properties") |
| 85 | assert.Empty(t, arrayField.Items.Schema.Required, "Items with $ref should not have required") |
nothing calls this directly
no test coverage detected
searching dependent graphs…