-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: combined properties, additional properties in named objects (#85)
* feat: allow combined properties and additional properties in toplevel objects * fix property names * added tests for generated json un-/marshaling code
- Loading branch information
Showing
11 changed files
with
638 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...nerators/models/testdata/cases/objects_with_properties_and_additional_properties/api.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 0.1.0 | ||
title: Test | ||
|
||
components: | ||
schemas: | ||
Foo: | ||
type: object | ||
properties: | ||
bar: | ||
type: integer | ||
requiredProperties: | ||
- bar | ||
additionalProperties: true | ||
FooBar: | ||
type: object | ||
properties: | ||
baz: | ||
type: string | ||
additionalProperties: | ||
type: array | ||
items: | ||
type: object | ||
properties: | ||
test: | ||
type: string |
100 changes: 100 additions & 0 deletions
100
...ls/testdata/cases/objects_with_properties_and_additional_properties/expected/model_foo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// This file is auto-generated, DO NOT EDIT. | ||
// | ||
// Source: | ||
// | ||
// Title: Test | ||
// Version: 0.1.0 | ||
package generatortest | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
) | ||
|
||
// Foo is an object. | ||
type Foo struct { | ||
FooProperties | ||
AdditionalProperties map[string]interface{} | ||
} | ||
|
||
type FooProperties struct { | ||
// Bar: | ||
Bar int32 `json:"bar,omitempty"` | ||
} | ||
|
||
// Unmarshal all named properties into FooProperties and | ||
// the rest into the AdditionalProperties map | ||
func (obj *Foo) UnmarshalJSON(data []byte) error { | ||
var generic map[string]json.RawMessage | ||
if err := json.Unmarshal(data, &generic); err != nil { | ||
return err | ||
} | ||
|
||
obj.FooProperties = FooProperties{} | ||
|
||
var additionalProperties = make(map[string]interface{}) | ||
for k, v := range generic { | ||
if k == "bar" { | ||
if err := json.Unmarshal(v, &(obj.FooProperties.Bar)); err != nil { | ||
return err | ||
} | ||
continue | ||
} | ||
var prop interface{} | ||
if err := json.Unmarshal(v, &prop); err != nil { | ||
return err | ||
} | ||
additionalProperties[k] = prop | ||
} | ||
|
||
obj.AdditionalProperties = additionalProperties | ||
return nil | ||
} | ||
|
||
// Marshal Foo by combining the AdditionalProperties with the | ||
// named properties in a single JSON object. Named properties take | ||
// precedence. | ||
func (obj Foo) MarshalJSON() ([]byte, error) { | ||
props := make(map[string]json.RawMessage) | ||
|
||
// start with additional properties so regular properties overwrite them | ||
for k, v := range obj.AdditionalProperties { | ||
if propData, err := json.Marshal(v); err == nil { | ||
props[k] = propData | ||
} else { | ||
return nil, err | ||
} | ||
} | ||
if propData, err := json.Marshal(obj.FooProperties.Bar); err == nil { | ||
props["bar"] = propData | ||
} else { | ||
return nil, err | ||
} | ||
|
||
data, err := json.Marshal(props) | ||
return data, err | ||
} | ||
|
||
// Validate implements basic validation for this model | ||
func (m Foo) Validate() error { | ||
return validation.Errors{}.Filter() | ||
} | ||
|
||
// GetBar returns the Bar property | ||
func (m Foo) GetBar() int32 { | ||
return m.Bar | ||
} | ||
|
||
// SetBar sets the Bar property | ||
func (m *Foo) SetBar(val int32) { | ||
m.Bar = val | ||
} | ||
|
||
func (m *Foo) GetAdditionalProperties() map[string]interface{} { | ||
return m.AdditionalProperties | ||
} | ||
|
||
func (m *Foo) SetAdditionalProperties(val map[string]interface{}) { | ||
m.AdditionalProperties = val | ||
} |
Oops, something went wrong.