generate icon indicating copy to clipboard operation
generate copied to clipboard

Arrays of objects not supported in references

Open antoineco opened this issue 6 years ago • 2 comments

Parking this here until I (or somebody else) look into it.

Given the following JSON schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "House",
    "type": "object",
    "definitions": {
        "address": {
            "type": "object",
            "properties": {
                "number": { "type": "integer" },
                "postcode": { "type": "string" }
            }
        },
        "owners": {
            "type": "array",
            "items": {
                "type": "object",
                "title": "person",
                "properties": {
                    "name": { "type": "string" }
                }
            }
        }
    },
    "properties": {
        "address": { "$ref": "#/definitions/address" },
        "owners": { "$ref": "#/definitions/owners" }
    }
}

The generated Owners field is not an array.

Moving definitions directly under properties almost* yields the expected result.

* minus the pointer symbol

Result:

// ...

// House 
type House struct {
  Address *Address `json:"address,omitempty"`
  Owners *Person `json:"owners,omitempty"`  // WRONG
                                            // should be []*Person
}

// ...

Expected:

// ...

// House 
type House struct {
  Address *Address `json:"address,omitempty"`
  Owners []*Person `json:"owners,omitempty"`
}

// ...

antoineco avatar Mar 26 '18 12:03 antoineco

Any idea on when the fix for this will be available?

WTIGER001 avatar May 20 '18 22:05 WTIGER001

I dug in here because I thought I hit this bug, but it was a user error.

AFACIT, this works correctly now; if I put the OP code into test.schema.json, I get the following:

➜ schema-generate test.schema.json
// Code generated by schema-generate. DO NOT EDIT.

package main

// Address
type Address struct {
  Number int `json:"number,omitempty"`
  Postcode string `json:"postcode,omitempty"`
}

// House
type House struct {
  Address *Address `json:"address,omitempty"`
  Owners []*Person `json:"owners,omitempty"`
}

// Person
type Person struct {
  Name string `json:"name,omitempty"`
}

ClaytonJY avatar Apr 07 '20 20:04 ClaytonJY