Skip to content

Commit

Permalink
Merge pull request #3 from birotaio/fix/enum
Browse files Browse the repository at this point in the history
Fix/enum
  • Loading branch information
fifteen-clement authored Apr 15, 2024
2 parents 216446f + 09a186f commit 972a27d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
16 changes: 5 additions & 11 deletions openapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gojsonschema

import "github.com/getkin/kin-openapi/openapi3"
import (
"github.com/getkin/kin-openapi/openapi3"
)

func GetOpenAPI(ls JSONLoader) (*openapi3.Schema, error) {
schema, err := NewSchema(ls)
Expand Down Expand Up @@ -43,8 +45,8 @@ func recursiveOpenApi(currentSubSchema *subSchema) *openapi3.Schema {
}

if schema != nil {
if len(currentSubSchema.enum) > 0 {
schema.WithEnum(ToInterface(currentSubSchema.enum)...)
if len(currentSubSchema.rawEnum) > 0 {
schema.WithEnum(currentSubSchema.rawEnum...)
}
if currentSubSchema.readOnly {
schema.ReadOnly = true
Expand All @@ -56,11 +58,3 @@ func recursiveOpenApi(currentSubSchema *subSchema) *openapi3.Schema {

return schema
}

func ToInterface[T any](a []T) []any {
res := make([]any, 0)
for _, obj := range a {
res = append(res, obj)
}
return res
}
54 changes: 54 additions & 0 deletions openapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gojsonschema

import (
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const Openapi string = `
{
"type": "object",
"properties": {
"string": {
"type": ["string", "null"],
"enum": [
"hello",
"world",
null
]
}
}
}
`

func TestOpenAPI(t *testing.T) {
var testCases = []struct {
schema string
}{
{
schema: Openapi,
},
}

// Check enum is well formated

for _, testCase := range testCases {
schema := NewStringLoader(testCase.schema)

res, err := GetOpenAPI(schema)
require.NoError(t, err)

a := &openapi3.T{}

a.Components = &openapi3.Components{}
a.Components.Schemas = make(openapi3.Schemas)
a.Components.Schemas["hihi"] = openapi3.NewSchemaRef("", res)
data, err := a.MarshalJSON()
require.NoError(t, err)
assert.JSONEq(t, `{"components":{"schemas":{"hihi":{"properties":{"string":{"enum":["hello","world",null],"nullable":true,"type":"string"}},"type":"object"}}},"info":null,"openapi":"","paths":null}`, string(data))
}
}
1 change: 1 addition & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema)
))
}
currentSchema.enum = append(currentSchema.enum, *is)
currentSchema.rawEnum = append(currentSchema.rawEnum, v)
}
} else {
return errors.New(formatErrorDescription(
Expand Down
5 changes: 3 additions & 2 deletions subSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ type subSchema struct {
additionalItems interface{}

// validation : all
_const *string //const is a golang keyword
enum []string
_const *string //const is a golang keyword
enum []string
rawEnum []any

// validation : subSchema
oneOf []*subSchema
Expand Down

0 comments on commit 972a27d

Please sign in to comment.