Skip to content

default schema attribute changes pointer behavior for booleans #478

@jessepeterson

Description

@jessepeterson

Hello! I use go-jsonschema in "models only" mode. I notice that when a default is present or not in a schema on a boolean value this will change its pointer behavior — even for a optional/omitempty field. This isn't such a big deal for a default-false boolean field (because the omitempy tag is still there) but when a default-true boolean is not a pointer this means if the source JSON does not have that field present then it gets set to the zero value of false, which doesn't seem correct for a default-true value.

I.e. schema (with no required property):

{
  "properties":{
    "example":{
      "type":"boolean",
      "default":true
    }
  }
}

Will result in a struct with a field:

type Struct struct {
  Example bool `json:"example,omitempty"`
}

But if the input JSON being parsed is:

{}

You'll end up with Example not only present but set to false—the incorrect default type.

Also it seems if any default is present (regardless of required being present or not) then it changes to a non-pointer. But, again, this seems to make the value present when a source struct may not have had the original key.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions