Code Monkey home page Code Monkey logo

Comments (1)

omissis avatar omissis commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.