Skip to content

Commit af1096a

Browse files
authored
add support for toplevel models that are map[string]Something (#10)
* add support for toplevel models that are map[string]Something. Before this change all objects with no props on the toplevel got rendered to map[string]interface{}.
1 parent 18de667 commit af1096a

File tree

12 files changed

+183
-1
lines changed

12 files changed

+183
-1
lines changed

pkg/generators/models/models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ func NewModelFromRef(ref *openapi3.SchemaRef) (model *Model, err error) {
8585
len(ref.Value.OneOf) > 0:
8686
model.Kind = Struct
8787
model.Properties, model.Imports, err = structPropsFromRef(ref)
88+
if len(model.Properties) == 0 {
89+
model.GoType = "map[string]interface{}"
90+
if ref.Value.AdditionalProperties != nil {
91+
model.GoType = "map[string]" + goTypeFromSpec(ref.Value.AdditionalProperties)
92+
}
93+
}
8894
default:
8995
return nil, errors.New("type not handled")
9096
}

pkg/generators/models/models_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func TestModels(t *testing.T) {
7575
name: "an untyped object may have additional properties of a specific type",
7676
directory: "testdata/cases/object_with_additional_properties",
7777
},
78+
{
79+
name: "an untyped toplevel object may have additional properties of a specific type",
80+
directory: "testdata/cases/toplevel_object_with_additional_properties",
81+
},
7882
{
7983
name: "handles string enums",
8084
directory: "testdata/cases/enum",

pkg/generators/models/templates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import (
4949
5050
{{ (printf "%s is an object. %s" .Name .Description) | commentBlock }}
5151
{{- if not .Properties }}
52-
type {{.Name}} map[string]interface{}
52+
type {{.Name}} {{ .GoType }}
5353
{{- else }}
5454
type {{.Name}} struct {
5555
{{- range .Properties}}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 0.1.0
4+
title: Test
5+
6+
components:
7+
schemas:
8+
Foo:
9+
type: object
10+
additionalProperties:
11+
type: string
12+
Bar:
13+
type: object
14+
additionalProperties:
15+
$ref: "#/components/schemas/Foo"
16+
Baz:
17+
type: object
18+
additionalProperties:
19+
type: array
20+
items:
21+
$ref: "#/components/schemas/Bar"
22+
Quak:
23+
type: object
24+
additionalProperties:
25+
oneof:
26+
- $ref: "#/components/schemas/Foo"
27+
- $ref: "#/components/schemas/Bar"
28+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is auto-generated, DO NOT EDIT.
2+
//
3+
// Source:
4+
// Title: Test
5+
// Version: 0.1.0
6+
package generatortest
7+
8+
import (
9+
validation "github.com/go-ozzo/ozzo-validation/v4"
10+
)
11+
12+
// Bar is an object.
13+
type Bar map[string]Foo
14+
15+
// Validate implements basic validation for this model
16+
func (m Bar) Validate() error {
17+
return validation.Errors{}.Filter()
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is auto-generated, DO NOT EDIT.
2+
//
3+
// Source:
4+
// Title: Test
5+
// Version: 0.1.0
6+
package generatortest
7+
8+
import (
9+
validation "github.com/go-ozzo/ozzo-validation/v4"
10+
)
11+
12+
// Baz is an object.
13+
type Baz map[string][]Bar
14+
15+
// Validate implements basic validation for this model
16+
func (m Baz) Validate() error {
17+
return validation.Errors{}.Filter()
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is auto-generated, DO NOT EDIT.
2+
//
3+
// Source:
4+
// Title: Test
5+
// Version: 0.1.0
6+
package generatortest
7+
8+
import (
9+
validation "github.com/go-ozzo/ozzo-validation/v4"
10+
)
11+
12+
// Foo is an object.
13+
type Foo map[string]string
14+
15+
// Validate implements basic validation for this model
16+
func (m Foo) Validate() error {
17+
return validation.Errors{}.Filter()
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is auto-generated, DO NOT EDIT.
2+
//
3+
// Source:
4+
// Title: Test
5+
// Version: 0.1.0
6+
package generatortest
7+
8+
import (
9+
validation "github.com/go-ozzo/ozzo-validation/v4"
10+
)
11+
12+
// Quak is an object.
13+
type Quak map[string]interface{}
14+
15+
// Validate implements basic validation for this model
16+
func (m Quak) Validate() error {
17+
return validation.Errors{}.Filter()
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is auto-generated, DO NOT EDIT.
2+
//
3+
// Source:
4+
// Title: Test
5+
// Version: 0.1.0
6+
package generatortest
7+
8+
import (
9+
validation "github.com/go-ozzo/ozzo-validation/v4"
10+
)
11+
12+
// Bar is an object.
13+
type Bar map[string]Foo
14+
15+
// Validate implements basic validation for this model
16+
func (m Bar) Validate() error {
17+
return validation.Errors{}.Filter()
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This file is auto-generated, DO NOT EDIT.
2+
//
3+
// Source:
4+
// Title: Test
5+
// Version: 0.1.0
6+
package generatortest
7+
8+
import (
9+
validation "github.com/go-ozzo/ozzo-validation/v4"
10+
)
11+
12+
// Baz is an object.
13+
type Baz map[string][]Bar
14+
15+
// Validate implements basic validation for this model
16+
func (m Baz) Validate() error {
17+
return validation.Errors{}.Filter()
18+
}

0 commit comments

Comments
 (0)