Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions schemav3.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ func BuildCustomSchemaV3(types []string) (*spec.RefOrSpec[spec.Schema], error) {
return nil, err
}

// TODO: check if this is correct
result := spec.NewSchemaSpec()
result.Spec.Type = &spec.SingleOrArray[string]{ARRAY}
result.Spec.AdditionalProperties = spec.NewBoolOrSchema(true, schema)
result.Spec.Items = spec.NewBoolOrSchema(false, schema)

return result, nil
case OBJECT:
Expand Down
45 changes: 45 additions & 0 deletions schemav3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package swag

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/sv-tools/openapi/spec"
)

func TestBuildCustomSchemaV3(t *testing.T) {
t.Parallel()

t.Run("array of primitive puts the element schema in items", func(t *testing.T) {
t.Parallel()
schema, err := BuildCustomSchemaV3([]string{"array", "integer"})
assert.NoError(t, err)
assert.Equal(t, &spec.SingleOrArray[string]{ARRAY}, schema.Spec.Type)
assert.Nil(t, schema.Spec.AdditionalProperties)
if assert.NotNil(t, schema.Spec.Items) && assert.NotNil(t, schema.Spec.Items.Schema) {
assert.Equal(t, &spec.SingleOrArray[string]{INTEGER}, schema.Spec.Items.Schema.Spec.Type)
}
})

t.Run("array without item type errors", func(t *testing.T) {
t.Parallel()
_, err := BuildCustomSchemaV3([]string{"array"})
assert.Error(t, err)
})

t.Run("object of primitive keeps additionalProperties", func(t *testing.T) {
t.Parallel()
schema, err := BuildCustomSchemaV3([]string{"object", "string"})
assert.NoError(t, err)
assert.Equal(t, &spec.SingleOrArray[string]{OBJECT}, schema.Spec.Type)
assert.NotNil(t, schema.Spec.AdditionalProperties)
assert.Nil(t, schema.Spec.Items)
})

t.Run("primitive", func(t *testing.T) {
t.Parallel()
schema, err := BuildCustomSchemaV3([]string{"string"})
assert.NoError(t, err)
assert.Equal(t, &spec.SingleOrArray[string]{STRING}, schema.Spec.Type)
})
}