Skip to content

Commit 5662f8c

Browse files
authored
Fix enums placement (#124)
* Fix enums placement (related to swaggest/openapi-go#144) * Add some examples in tests * Lint issues fix
1 parent 2c385a3 commit 5662f8c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

reflect.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,17 +1379,35 @@ func reflectEnum(schema *Schema, fieldTag reflect.StructTag, fieldVal interface{
13791379
enum.loadFromField(fieldTag, fieldVal)
13801380

13811381
if len(enum.items) > 0 {
1382-
schema.Enum = enum.items
1382+
target := searchDeepestSchema(schema)
1383+
target.Enum = enum.items
1384+
13831385
if len(enum.names) > 0 {
1384-
if schema.ExtraProperties == nil {
1385-
schema.ExtraProperties = make(map[string]interface{}, 1)
1386+
if target.ExtraProperties == nil {
1387+
target.ExtraProperties = make(map[string]interface{}, 1)
13861388
}
13871389

13881390
schema.ExtraProperties[XEnumNames] = enum.names
13891391
}
13901392
}
13911393
}
13921394

1395+
func searchDeepestSchema(in *Schema) *Schema {
1396+
if in.Items == nil {
1397+
return in
1398+
}
1399+
1400+
if in.Items.SchemaOrBool == nil {
1401+
return in
1402+
}
1403+
1404+
if in.Items.SchemaOrBool.TypeObject == nil {
1405+
return in
1406+
}
1407+
1408+
return searchDeepestSchema(in.Items.SchemaOrBool.TypeObject)
1409+
}
1410+
13931411
// enum can be use for sending enum data that need validate.
13941412
type enum struct {
13951413
items []interface{}

reflect_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,3 +2655,34 @@ func TestReflector_Reflect_byteSlice(t *testing.T) {
26552655
"type":"object"
26562656
}`, s)
26572657
}
2658+
2659+
func TestReflector_Reflect_EnumsPlacement(t *testing.T) {
2660+
r := jsonschema.Reflector{}
2661+
2662+
type Check struct {
2663+
A int `json:"a" enum:"1"`
2664+
B []string `json:"b" enum:"check-string"`
2665+
C []withValNamedEnum `json:"c"`
2666+
D [][][]string `json:"d" enum:"d"`
2667+
F map[string]string `json:"f" enum:"f"`
2668+
G map[int]map[string][]int `json:"g" enum:"1"`
2669+
}
2670+
2671+
got, err := r.Reflect(Check{})
2672+
require.NoError(t, err)
2673+
2674+
assertjson.EqMarshal(t, `{
2675+
"definitions": {
2676+
"JsonschemaGoTestWithValNamedEnum": {"enum": [""],"type": "string","x-enum-names": ["n:"]}
2677+
},
2678+
"properties":{
2679+
"a":{"enum":["1"],"type":"integer"},
2680+
"b":{"items":{"enum":["check-string"],"type":"string"},"type":["array","null"]},
2681+
"c":{"items":{"$ref":"#/definitions/JsonschemaGoTestWithValNamedEnum"},"type":["array","null"]},
2682+
"d":{"items":{"items":{"items":{"enum":["d"],"type":"string"},"type":"array"},"type":"array"},"type":["array","null"]},
2683+
"f":{"additionalProperties":{"type":"string"},"enum":["f"],"type":["object","null"]},
2684+
"g":{"additionalProperties":{"additionalProperties":{"items":{"type":"integer"},"type":"array"},"type":"object"},"enum":["1"],"type":["object","null"]}
2685+
},
2686+
"type":"object"
2687+
}`, got)
2688+
}

0 commit comments

Comments
 (0)