In our project we process a large number of JSON files from external source. We want to use pydantic for data models and also to check the structure of incoming files. The JSON files has spaces and special characters in the keys.
Example:
{
"Serial Number": "A12345678",
"Timestamp": "2020-05-26T12:15:25.792741Z",
"Data": {
"Length (m)": 12.34,
"Symmetric deviation (%)": 12.216564148290807,
"Total running time (s)": 974,
"Mass (kg)": 42.23,
"Initial parameters": {
"V1": 123,
"V2": 456
}
}
}
The keys with space and brackets works perfect. They are generated as Length__m_: float = Field(..., alias='Length (m)') for example.
The generator don't likes the "Data.Initial parameters" key in the example above. It is the name of the sub structure and would result in a class name with space The following error is raised:
black.InvalidInput: Cannot parse: 6:14: class Initial parameters(BaseModel):
It would be cool, if the generator would support such files. Is this possible to add this feature?
@fzirker
Thank you for writing detail about the problem.
I have understood. I will re-use converting logic of field name for generating model name.
I'm sorry, I have some tasks. I want to implement this issue in this week or next week.
@fzirker
I have released a fixed version as 0.5.12 :tada:
Hi @koxudaxi thank your for your work.
Unfortunately I have a problem with the new version. The fields with spaces are now optional.
Example created with the new version 0.5.12 (from sample above):
Serial_Number: Optional[str] = Field(None, alias='Serial Number')
With old version 0.5.11 the field were defined like this.
Serial_Number: str = Field(..., alias='Serial Number')
@fzirker
Oh, I'm sorry, I mistake it.
I have fixed and released it as 0.5.13
I've tested it now, works perfectly now.
Thank your very much.
edit: SOLVED: I discovered x-enum-vars. thanks!
I have a schema in an openapi 3 yaml file:
components:
schemas:
EmissionUnit:
type: "string"
enum: ["Kg COâ‚‚", "Kg COâ‚‚e", "tonnes COâ‚‚e"]
and I get :
black.InvalidInput: Cannot parse: 59:9: Kg_COâ‚‚ = 'Kg COâ‚‚'
Most helpful comment
I've tested it now, works perfectly now.
Thank your very much.