apitizer icon indicating copy to clipboard operation
apitizer copied to clipboard

Bad mock generation when referencing schema on array

Open jonneves opened this issue 9 years ago • 6 comments

Hello @retro,

First things first, congrats on the awesome lib.

I'm currently working on a project where I've to generate the JSON schema from the services definition and then generate Mocks for the services based on that schema. Your lib has been a great tool for me.

I'm only having problems with references when on an array.

If a use the same reference on a simple property and all works great.

I've uploaded my schema and the result.

Can you help me on this one?

Best Regards, Jonatan

result-order.schema.json.txt order.schema.json.txt

jonneves avatar Jun 27 '16 21:06 jonneves

Just to make easier,

Using the schema from the reference wiki page, just changing to an array:

{
    type: "object",
    definitions: {
        address: {
            type: "object",
            properties: {
                street_address: {
                    "type": "string"
                },
                city: {
                    "type": "string"
                },
                state: {
                    "type": "string"
                },
                phoneNumbers: {
                    "type": "array",
                    items: {
                        type: "integer",
                        minimum: 1000000,
                        maximum: 9999999
                    }
                },
                country: {
                    oneOf: [{
                        type: "object",
                        properties: {
                            postal: {
                                type: "integer",
                                minimum: 100,
                                maximum: 999
                            },
                            name: {
                                type: "string"
                            }
                        }
                    }, {
                        type: "string"
                    }]
                }
            },
            required: ["street_address", "city", "state"]
        }
    },
    properties: {
        billing_address: {
            "type" : "array",
            "items" : {
                "$ref": "#/definitions/address"
            }
        },
        shipping_address: {
            allOf: [{
                $ref: "#/definitions/address"
            }, {
                properties: {
                    type: {
                        enum: ["residential", "business"]
                    }
                },
                required: ["type"]
            }]
        }
    }
}

And this is the result:

{
    "billing_address": [
        {
            "street_address": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "city": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "state": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "phoneNumbers": {
                "opts": {
                    "maxItems": 10,
                    "minItems": 0,
                    "uniqueItems": false,
                    "typeDef": {
                        "opts": {
                            "minimum": 1000000,
                            "maximum": 9999999,
                            "multipleOf": null,
                            "exclusiveMaximum": false,
                            "exclusiveMinimum": false,
                            "type": "integer"
                        }
                    },
                    "type": "array",
                    "items": {
                        "type": "integer",
                        "minimum": 1000000,
                        "maximum": 9999999
                    }
                }
            },
            "country": {}
        },
        {
            "street_address": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "city": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "state": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "phoneNumbers": {
                "opts": {
                    "maxItems": 10,
                    "minItems": 0,
                    "uniqueItems": false,
                    "typeDef": {
                        "opts": {
                            "minimum": 1000000,
                            "maximum": 9999999,
                            "multipleOf": null,
                            "exclusiveMaximum": false,
                            "exclusiveMinimum": false,
                            "type": "integer"
                        }
                    },
                    "type": "array",
                    "items": {
                        "type": "integer",
                        "minimum": 1000000,
                        "maximum": 9999999
                    }
                }
            },
            "country": {}
        },
        {
            "street_address": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "city": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "state": {
                "opts": {
                    "maxLength": null,
                    "minLength": 0,
                    "type": "string"
                }
            },
            "phoneNumbers": {
                "opts": {
                    "maxItems": 10,
                    "minItems": 0,
                    "uniqueItems": false,
                    "typeDef": {
                        "opts": {
                            "minimum": 1000000,
                            "maximum": 9999999,
                            "multipleOf": null,
                            "exclusiveMaximum": false,
                            "exclusiveMinimum": false,
                            "type": "integer"
                        }
                    },
                    "type": "array",
                    "items": {
                        "type": "integer",
                        "minimum": 1000000,
                        "maximum": 9999999
                    }
                }
            },
            "country": {}
        }
    ],
    "shipping_address": {
        "type": "residential",
        "street_address": "Lorem ipsum",
        "city": "Lorem ipsum",
        "state": "Lorem ipsum",
        "phoneNumbers": [
            1659581,
            5538032,
            5135738,
            5977773,
            8122058,
            8231341,
            8303215
        ],
        "country": {
            "postal": 551,
            "name": "Lorem ipsum"
        }
    }
}

jonneves avatar Jun 27 '16 21:06 jonneves

Does it work correctly if you're not using $ref and you just copy / paste the address schema?

retro avatar Jun 29 '16 18:06 retro

No, it does not.

If a change my items to:

"customerDetails" : {
                    "type": "array",
                    "items": {
                       "type" : "object",
                        "id" : "contactInfo",
                        "properties" : {
                            "firstName" : {
                                "type": "string"
                            },
                            "lastName" : {
                                "type": "string"
                            },
                            "telephone" : {
                                "type": "string"
                            },
                            "email" : {
                                "type": "string"
                            }
                        },
                        "required" : ["firstName","lastName"]
                    }
                }

I'll get the same output:

"customer": {
        "accountNumber": "Lorem ipsum",
        "customerDetails": [
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            },
            {
                "firstName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "lastName": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "telephone": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                },
                "email": {
                    "opts": {
                        "maxLength": null,
                        "minLength": 0,
                        "type": "string"
                    }
                }
            }
        ]
    },

Regards.

jonneves avatar Jun 29 '16 19:06 jonneves

If I take out the array type and the items, and left just one occurrence it works both with $ref or copying the object itself.

jonneves avatar Jun 29 '16 19:06 jonneves

Hi,

Do you have any news about this issue? There's a workaround or something that I can do?

Thanks, Jonatan

jonneves avatar Jul 21 '16 15:07 jonneves

Hi,

Unfortunately I didn't have time to look at this yet. I'll try to find some time next week.

retro avatar Jul 22 '16 19:07 retro