Comments (2)
I'm facing the same problem, no additionalProperties
are being unmarshalled, rendering them unusable.
E.g. having a schema.json
{
"type": "object",
"additionalProperties": {
"type": "object",
"properties": {
"property1": {
"type": "string"
},
"property2": {
"type": "number"
}
}
},
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "string"
}
}
}
after calling go-jsonschema schema.json --output schema.go --package gen --tags yaml --extra-imports
the Go code is:
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.
package gen
import "encoding/json"
import yaml "gopkg.in/yaml.v3"
type TestJson struct {
// Bar corresponds to the JSON schema field "bar".
Bar *string `yaml:"bar,omitempty"`
// Foo corresponds to the JSON schema field "foo".
Foo *string `yaml:"foo,omitempty"`
AdditionalProperties map[string]interface{}
}
// UnmarshalJSON implements json.Unmarshaler.
func (j *TestJson) UnmarshalJSON(b []byte) error {
var raw map[string]interface{}
if err := json.Unmarshal(b, &raw); err != nil {
return err
}
type Plain TestJson
var plain Plain
if err := json.Unmarshal(b, &plain); err != nil {
return err
}
if v, ok := raw[""]; !ok || v == nil {
plain.AdditionalProperties = map[string]interface{}{}
}
*j = TestJson(plain)
return nil
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (j *TestJson) UnmarshalYAML(value *yaml.Node) error {
var raw map[string]interface{}
if err := value.Decode(&raw); err != nil {
return err
}
type Plain TestJson
var plain Plain
if err := value.Decode(&plain); err != nil {
return err
}
if v, ok := raw[""]; !ok || v == nil {
plain.AdditionalProperties = map[string]interface{}{}
}
*j = TestJson(plain)
return nil
}
Then a test unmarshalling the following test.yaml
:
foo: "foo value"
bar: "bar value"
property1: "additional property 1"
property2: "additional property 2"
fails since "property1" and "property2" are not read into Go struct.
Go test code:
t.Run("test", func(t *testing.T) {
var yamlNode yaml3.Node
file, _ := os.ReadFile("test.yaml")
yaml3.Unmarshal(file, &yamlNode)
genStruct := gen.TestJson{}
genStruct.UnmarshalYAML(&yamlNode)
if len(genStruct.AdditionalProperties) == 0 {
t.Fatalf("no additional properties")
}
})
from go-jsonschema.
Hey @jamietanna, thanks for opening this issue. I am not sure I understand this use-case, could you elaborate a bit more please? eg: why would additional properties need to be omitted? and how does including them causes the failure in capturing the required fields?
Perhaps a full marshaling+unmarshaling example could help, thanks! 😁
from go-jsonschema.
Related Issues (20)
- Go install complains on go.mod HOT 4
- `maxLength` is not working as expected according to the jsonschema docs HOT 3
- Generated Unmarshal for fields with `time.Time` HOT 5
- How to specify a root directory for `$ref` resolution? HOT 1
- CVE Schema: could not generate type for field "...": definition does not exist in schema: HOT 6
- feat: Use of project as package
- Nested struct support? HOT 1
- Fails to generate for Signal K, cannot generate referenced type. HOT 1
- Nullable nested object support
- Incorrect string length validation for some strings. HOT 1
- Does not generate anything for the GeoJson schema
- improve support for additionalProperties HOT 1
- Problems generating structs from schema. HOT 1
- No option to indicate an empty value for optional array field
- Feature request: string pattern validation HOT 4
- Add support for `MarshalJSON` HOT 4
- Go module import path needs an update after repo move HOT 2
- Validation breaks unmarhalling into non-empty struct
- go-jsonschema: Failed: error parsing from file ./Cluster: cannot load schema error parsing JSON file Cluster: failed to unmarshal JSON: invalid character '+' in string escape code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-jsonschema.