From 09a186f58dbdf39a8e5dbaea22daa867539f14f5 Mon Sep 17 00:00:00 2001 From: fifteen-clement Date: Mon, 15 Apr 2024 10:31:00 +0200 Subject: [PATCH] fix: enum generation --- openapi.go | 16 +++++---------- openapi_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ schema.go | 1 + subSchema.go | 5 +++-- 4 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 openapi_test.go diff --git a/openapi.go b/openapi.go index 42d2678..a1427a2 100644 --- a/openapi.go +++ b/openapi.go @@ -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) @@ -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 @@ -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 -} diff --git a/openapi_test.go b/openapi_test.go new file mode 100644 index 0000000..ca90187 --- /dev/null +++ b/openapi_test.go @@ -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)) + } +} diff --git a/schema.go b/schema.go index 4c09aab..a67a0b7 100644 --- a/schema.go +++ b/schema.go @@ -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( diff --git a/subSchema.go b/subSchema.go index d46399e..2e1d87e 100644 --- a/subSchema.go +++ b/subSchema.go @@ -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