Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]default value of number will get int #38

Closed
hsz1273327 opened this issue Jul 25, 2022 · 4 comments · Fixed by #93
Closed

[bug]default value of number will get int #38

hsz1273327 opened this issue Jul 25, 2022 · 4 comments · Fixed by #93

Comments

@hsz1273327
Copy link

so if using the default value,it can not set into the struct .
截屏2022-07-25 下午4 11 22
截屏2022-07-25 下午4 11 42

@samlown
Copy link
Contributor

samlown commented Jul 29, 2022

Hi! Not sure I understand your problem here. Could you give an example of what is not working as expected? Thanks.

@hsz1273327
Copy link
Author

for example:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/invopop/jsonschema"
)

type SampleUser struct {
	FloatExp float64 `json:"floatexp" jsonschema:"default=12.3"`
}

func main() {
	s := jsonschema.Reflect(&SampleUser{})
	data, err := json.MarshalIndent(s, "", "  ")
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(string(data))
}

will get:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$ref": "#/$defs/SampleUser",
  "$defs": {
    "SampleUser": {
      "properties": {
        "floatexp": {
          "type": "number",
          "default": 0
        }
      },
      "additionalProperties": false,
      "type": "object",
      "required": [
        "floatexp"
      ]
    }
  }
}

@HarunOr
Copy link

HarunOr commented Aug 6, 2022

Hi! Not sure I understand your problem here. Could you give an example of what is not working as expected? Thanks.

The default values set in struct tags get overwritten and are always 0 for float types.
This PR seems to fix it though.

@hsz1273327
Copy link
Author

send a pull request #55 to fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants