datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Show more precise cause of "Invalid file format" exception

Open deepaerial opened this issue 4 years ago • 7 comments
trafficstars

Is your feature request related to a problem? Please describe.

I have RFC 8259 valid JSON (not jsonschema type) file with list of objects. When I try to run datamodel-codegen --input list_of_leads.json --input-file-type json --output leads_model.py command I get "Invalid file format" message. Interesting thing is that script works if list has one object in it. I think it fails because of some object in the list, but because list I'm feeding to datamodel-codegen has thousands of records I can't determine which one causes this error.

Describe the solution you'd like

I would like datamodel-codegen to show more detailed trace for "Invalid file format" exception. The best solution would be displaying whole JSON object with pointer to column or some error message explaining why this record can't be processed.

Describe alternatives you've considered For example tool can at least display column name or specific record count number that cannot be processed.

deepaerial avatar Oct 07 '21 13:10 deepaerial

@deepaerial I'm sorry for my late reply. Thank you for creating this issue. It's a great idea. But, It may be difficult to realize it 🤔 I will think about how to implement it.

koxudaxi avatar Oct 26 '21 01:10 koxudaxi

For people stumbling upon this issue in the search for what might have gone wrong (such as myself), in my case it was an encoding issue.

The json file I was trying to run through datamodel-codegen was the output of jq encoded as UTF-8, whereas the input to jq was us-ascii encoded. In my case, a simple -a option was enough for jq to keep the original encoding, and then the output went through datamodel-codegen without issue.

You can see a file's encoding with the file -i command.

ddanielgal avatar Mar 16 '22 22:03 ddanielgal

I also ran into the Invalid file format issue, and it seems like it has something to do with using yaml as the input file format:

$ datamodel-codegen --output /tmp/x.py --input-file-type=yaml --input /tmp/x.yaml
Invalid file format

Converting the yaml to json seems to work, though:

import yaml
import json
json_obj = yaml.safe_load(open('/tmp/x.yaml').read())
open('/tmp/x.json', 'w', encoding='ascii').write(json.dumps(json_obj))

And then:

$ datamodel-codegen --output /tmp/x.py --input /tmp/x.json
… works …

wolever avatar Jul 26 '22 15:07 wolever

@deepaerial @wolever I'm sorry for my late reply. OK, I will improve the message and fallback action when the code-gen can't load the input file.

koxudaxi avatar Dec 31 '22 15:12 koxudaxi

default encoding for input file is cp1251

microspace avatar Nov 16 '23 09:11 microspace

I have same message, different root cause. I have root object was not list, but object seems. had to jq into list.

dzmitry-lahoda avatar Dec 19 '23 15:12 dzmitry-lahoda

I have same message. Perfectly valid openapi 3 yaml file had this content:

      example:
        field:
          - change: =
            content: status
        operator:
          - change: =
            content: less_than
        value:
          - change: +
            content: solved

In order to get the model to generate, I needed to enclose the equal signs in quotes.

     example:
       field:
         - change: '='
           content: status
       operator:
         - change: '='
           content: less_than
       value:
         - change: +
           content: solved

peterlynch avatar Jan 18 '24 14:01 peterlynch