Comments (1)
Hi, I was not able to reproduce this problem. Given the issue is relatively old, I assume it has been solved. I tried using the following schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://example.com/special-characters",
"type": "object",
"properties": {
"plainLicenses": {
"type": "string",
"enum": [
"GPL-3.0",
"MIT"
]
},
"plusLicenses": {
"type": "string",
"enum": [
"GPL-3.0+",
"MIT+"
]
},
"plainLicensesRef": {
"type": "array",
"items": {
"$ref": "#/definitions/license"
}
},
"plusLicensesRef": {
"type": "array",
"items": {
"$ref": "#/definitions/license+"
}
}
},
"definitions": {
"license": {
"type": "string",
"enum": [
"GPL-3.0",
"MIT"
]
},
"license+": {
"type": "string",
"enum": [
"GPL-3.0+",
"MIT+"
]
}
}
}
and I got the following output:
// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.
package test
import "encoding/json"
import "fmt"
import "reflect"
type License string
const LicenseGPL30 License = "GPL-3.0"
const LicenseMIT License = "MIT"
type License_1 string
const License_1_GPL30 License_1 = "GPL-3.0+"
const License_1_MIT License_1 = "MIT+"
type SpecialCharacters struct {
// PlainLicenses corresponds to the JSON schema field "plainLicenses".
PlainLicenses *SpecialCharactersPlainLicenses `json:"plainLicenses,omitempty" yaml:"plainLicenses,omitempty" mapstructure:"plainLicenses,omitempty"`
// PlainLicensesRef corresponds to the JSON schema field "plainLicensesRef".
PlainLicensesRef []License `json:"plainLicensesRef,omitempty" yaml:"plainLicensesRef,omitempty" mapstructure:"plainLicensesRef,omitempty"`
// PlusLicenses corresponds to the JSON schema field "plusLicenses".
PlusLicenses *SpecialCharactersPlusLicenses `json:"plusLicenses,omitempty" yaml:"plusLicenses,omitempty" mapstructure:"plusLicenses,omitempty"`
// PlusLicensesRef corresponds to the JSON schema field "plusLicensesRef".
PlusLicensesRef []License_1 `json:"plusLicensesRef,omitempty" yaml:"plusLicensesRef,omitempty" mapstructure:"plusLicensesRef,omitempty"`
}
type SpecialCharactersPlainLicenses string
const SpecialCharactersPlainLicensesGPL30 SpecialCharactersPlainLicenses = "GPL-3.0"
const SpecialCharactersPlainLicensesMIT SpecialCharactersPlainLicenses = "MIT"
type SpecialCharactersPlusLicenses string
const SpecialCharactersPlusLicensesGPL30 SpecialCharactersPlusLicenses = "GPL-3.0+"
const SpecialCharactersPlusLicensesMIT SpecialCharactersPlusLicenses = "MIT+"
var enumValues_License = []interface{}{
"GPL-3.0",
"MIT",
}
var enumValues_License_1 = []interface{}{
"GPL-3.0+",
"MIT+",
}
var enumValues_SpecialCharactersPlainLicenses = []interface{}{
"GPL-3.0",
"MIT",
}
var enumValues_SpecialCharactersPlusLicenses = []interface{}{
"GPL-3.0+",
"MIT+",
}
// UnmarshalJSON implements json.Unmarshaler.
func (j *SpecialCharactersPlusLicenses) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var ok bool
for _, expected := range enumValues_SpecialCharactersPlusLicenses {
if reflect.DeepEqual(v, expected) {
ok = true
break
}
}
if !ok {
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlusLicenses, v)
}
*j = SpecialCharactersPlusLicenses(v)
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (j *SpecialCharactersPlainLicenses) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var ok bool
for _, expected := range enumValues_SpecialCharactersPlainLicenses {
if reflect.DeepEqual(v, expected) {
ok = true
break
}
}
if !ok {
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_SpecialCharactersPlainLicenses, v)
}
*j = SpecialCharactersPlainLicenses(v)
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (j *License_1) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var ok bool
for _, expected := range enumValues_License_1 {
if reflect.DeepEqual(v, expected) {
ok = true
break
}
}
if !ok {
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License_1, v)
}
*j = License_1(v)
return nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (j *License) UnmarshalJSON(b []byte) error {
var v string
if err := json.Unmarshal(b, &v); err != nil {
return err
}
var ok bool
for _, expected := range enumValues_License {
if reflect.DeepEqual(v, expected) {
ok = true
break
}
}
if !ok {
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValues_License, v)
}
*j = License(v)
return nil
}
Feel free to reopen this issue if you have a case where you see this still happening.
from go-jsonschema.
Related Issues (20)
- Object Properties do not generate valid Unmarshal code
- Handling of `required` properties in objects HOT 1
- Handling duplicate types HOT 15
- Enum integers are generated as floats. HOT 5
- local file ref failure HOT 4
- Fails to generate schema for github-workflow HOT 3
- 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.
- No option to indicate an empty value for optional array field
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.