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

Generating illegal avro schema #36

Closed
vikusku opened this issue Feb 20, 2020 · 2 comments
Closed

Generating illegal avro schema #36

vikusku opened this issue Feb 20, 2020 · 2 comments

Comments

@vikusku
Copy link

vikusku commented Feb 20, 2020

I'm generation avro schema for AddressType struct

type AddressType struct {
	Street         *LocalizedString `json:"street,omitempty"`
	ZipCode        string           `json:"zipCode,omitempty"`
	MunicipalityID string           `json:"municipalityID,omitempty"`
	Municipality   *LocalizedString `json:"municipality,omitempty"`
	Country        *LocalizedString `json:"country,omitempty"`
}
type LocalizedString struct {
	FiFi string `json:"fi_FI,omitempty"`
	SvFi string `json:"sv_FI,omitempty"`
	EnGb string `json:"en_GB,omitempty"`
	EtEe string `json:"et_EE,omitempty"`
	RuRu string `json:"ru_RU,omitempty"`
}
func LocalizedStringSchema() (*avro.RecordSchema, error) {
	fiFi, err := avro.NewField("fi_FI", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	svFi, err := avro.NewField("sv_FI", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	enGb, err := avro.NewField("en_GB", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	etEe, err := avro.NewField("et_EE", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	ruRu, err := avro.NewField("ru_RU", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	fields := []*avro.Field{fiFi, svFi, enGb, etEe, ruRu}
	localizedStringSchema, err := avro.NewRecordSchema("LocalizedString", "", fields)
	if err != nil {
		return nil, err
	}

	return localizedStringSchema, nil
}

func AddressTypeSchema() (avro.Schema, error) {
	localizedString, err := LocalizedStringSchema()
	if err != nil {
		return nil, err
	}

	types := []avro.Schema{avro.NewPrimitiveSchema(avro.Null), localizedString}
	unionSchema, err :=  avro.NewUnionSchema(types)
	if err != nil {
		return nil, err
	}

	street, err := avro.NewField("street", unionSchema, nil)
	if err != nil {
		return nil, err
	}

	zipCode, err := avro.NewField("zipCode", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	municipalityID, err := avro.NewField("municipalityID", avro.NewPrimitiveSchema(avro.String), nil)
	if err != nil {
		return nil, err
	}

	municipality, err := avro.NewField("municipality", unionSchema, nil)
	if err != nil {
		return nil, err
	}

	country, err := avro.NewField("country", unionSchema, nil)
	if err != nil {
		return nil, err
	}

	fields := []*avro.Field{street, zipCode, municipalityID, municipality, country}
	addressTypeSchema, err := avro.NewRecordSchema("AddressType", namespace, fields)
	if err != nil {
		return nil, err
	}


	return addressTypeSchema, nil
}

When I'm trying to register this schema in confluent schema registry I get "Input schema is an invalid Avro schema" error. Could you please point me to what I'm doing wrong.

{
  "name": "fi.sok.schema.raflaamo.AddressType",
  "type": "record",
  "fields": [
    {
      "name": "street",
      "type": [
        "null",
        {
          "name": "LocalizedString",
          "type": "record",
          "fields": [
            {
              "name": "fi_FI",
              "type": "string"
            },
            {
              "name": "sv_FI",
              "type": "string"
            },
            {
              "name": "en_GB",
              "type": "string"
            },
            {
              "name": "et_EE",
              "type": "string"
            },
            {
              "name": "ru_RU",
              "type": "string"
            }
          ]
        }
      ]
    },
    {
      "name": "zipCode",
      "type": "string"
    },
    {
      "name": "municipalityID",
      "type": "string"
    },
    {
      "name": "municipality",
      "type": [
        "null",
        {
          "name": "LocalizedString",
          "type": "record",
          "fields": [
            {
              "name": "fi_FI",
              "type": "string"
            },
            {
              "name": "sv_FI",
              "type": "string"
            },
            {
              "name": "en_GB",
              "type": "string"
            },
            {
              "name": "et_EE",
              "type": "string"
            },
            {
              "name": "ru_RU",
              "type": "string"
            }
          ]
        }
      ]
    },
    {
      "name": "country",
      "type": [
        "null",
        {
          "name": "LocalizedString",
          "type": "record",
          "fields": [
            {
              "name": "fi_FI",
              "type": "string"
            },
            {
              "name": "sv_FI",
              "type": "string"
            },
            {
              "name": "en_GB",
              "type": "string"
            },
            {
              "name": "et_EE",
              "type": "string"
            },
            {
              "name": "ru_RU",
              "type": "string"
            }
          ]
        }
      ]
    }
  ]
}

@nrwiersma
Copy link
Member

Off the top of my head i would say that the second and third use of LocalizedString should be reference schemas. I think you redeclaring them is likely the issue, but I would need to play with it more to know for sure.

@vikusku
Copy link
Author

vikusku commented Feb 20, 2020

Yes, looks like that was an issue. Schema registration succeeded when using refs. I'll keep this issue open for a bit because I still get some errors. I'm not sure that it's related to avro schema so I need to investigate more.

@vikusku vikusku closed this as completed Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants