Skip to content

Commit

Permalink
Merge pull request #4 from birotaio/feature/enum_oneof
Browse files Browse the repository at this point in the history
handle oneof
  • Loading branch information
MathieuZoov authored May 31, 2024
2 parents 972a27d + 8856eca commit 81cbec2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
46 changes: 35 additions & 11 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,45 @@ func recursiveOpenApi(currentSubSchema *subSchema) *openapi3.Schema {
schema = openapi3.NewIntegerSchema()
} else if currentSubSchema.types.Contains(TYPE_STRING) {
schema = openapi3.NewStringSchema()
} else if currentSubSchema.types.Contains(TYPE_CONST) {
schema = &openapi3.Schema{Type: TYPE_CONST}
}

if currentSubSchema.format != "" {
schema.WithFormat(currentSubSchema.format)
}
if schema == nil {
return nil
}

if schema != nil {
if len(currentSubSchema.rawEnum) > 0 {
schema.WithEnum(currentSubSchema.rawEnum...)
}
if currentSubSchema.readOnly {
schema.ReadOnly = true
if currentSubSchema.format != "" {
schema.WithFormat(currentSubSchema.format)
}

if currentSubSchema._const != nil {
if schema.Extensions == nil {
schema.Extensions = make(map[string]interface{})
}
if currentSubSchema.types.Contains(TYPE_NULL) {
schema.Nullable = true

schema.Extensions["const"] = currentSubSchema.rawConst
}

if currentSubSchema.title != nil {
schema.Title = *currentSubSchema.title
}

if len(currentSubSchema.rawEnum) > 0 {
schema.WithEnum(currentSubSchema.rawEnum...)
}

if currentSubSchema.readOnly {
schema.ReadOnly = true
}

if currentSubSchema.types.Contains(TYPE_NULL) {
schema.Nullable = true
}

if len(currentSubSchema.oneOf) > 0 {
for _, value := range currentSubSchema.oneOf {
schema.OneOf = append(schema.OneOf, &openapi3.SchemaRef{Value: recursiveOpenApi(value)})
}
}

Expand Down
1 change: 1 addition & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema)
return err
}
currentSchema._const = is
currentSchema.rawConst = m[KEY_CONST]
}

if existsMapKey(m, KEY_ENUM) {
Expand Down
7 changes: 4 additions & 3 deletions subSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ type subSchema struct {
additionalItems interface{}

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

// validation : subSchema
oneOf []*subSchema
Expand Down
9 changes: 7 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
TYPE_NULL = `null`
TYPE_OBJECT = `object`
TYPE_STRING = `string`
TYPE_CONST = `const`
)

// JSON_TYPES hosts the list of type that are supported in JSON
Expand All @@ -50,13 +51,17 @@ func init() {
TYPE_NUMBER,
TYPE_NULL,
TYPE_OBJECT,
TYPE_STRING}
TYPE_STRING,
TYPE_CONST,
}

SCHEMA_TYPES = []string{
TYPE_ARRAY,
TYPE_BOOLEAN,
TYPE_INTEGER,
TYPE_NUMBER,
TYPE_OBJECT,
TYPE_STRING}
TYPE_STRING,
TYPE_CONST,
}
}

0 comments on commit 81cbec2

Please sign in to comment.