a-h / generate

Generates Go (golang) Structs from JSON schema.
MIT License
444 stars 137 forks source link

Stack overflow on big schema file #85

Closed apapsch closed 2 years ago

apapsch commented 2 years ago

When feeding schema-generate with a big schema file, it may abort with stack overflow.

Steps to reproduce:

  1. Download this schema file of Shopware Customer. It is ~266 KB big.
  2. The file is missing $schema and $id, I added the following bit (don't know if the schema is correct).
  3. Feed the augmented file to schema-generate and wait.
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://stoplight.io/api/v1/projects/shopware/admin-api/nodes/adminapi.json/components/schemas/Customer"

Result

The program aborts: overflow.log. schema-generate was built with go version go1.17.6 windows/amd64.

Expected Result

schema-generate does not crash, though it might exit with another error because I don't know if the example schema file is valid.

shynome commented 2 years ago

Hi, I have the same problem, how did you deal with it?

apapsch commented 2 years ago

This repo seems to be abandoned and I didn't want to dig into the code, architectural problems are hard to fix when you don't know the code. In the end I created a POC that sufficed for my need generating Go structs of Shopware schema file. I just extracted and dumped it here: https://codeberg.org/apapsch/go-jsonschema-model-gen Feel free to fork and adapt to your needs. It creates structs from the toplevel entity and any referenced entities.

In the Shopware case, I had to search-replace some mishaps in the generated code, but that was due to the schema being invalid itself: for collection fields they specify object where it should really be array of T (I guess it was due to them implementing collections in PHP using custom classes, which their schema generator didn't recognize as array of T).