[BUG] Api has parameter naming discrepency between generated models and API
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [x] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
Description
I have a django stack. When I run the command:
openapi-generator-cli generate \
-i http://localhost:8000/api/schema/ \
-g typescript-fetch \
-o ./assets/javascript/api-client/ \
It will generate the following in the file FinanciersApi.ts, which uses snake_case to define the omitted parameters:
export interface FinanciersApiFinancingOptionCreateRequest {
financingOption: Omit<
FinancingOption,
'id' | 'created_at' | 'updated_at' | 'foo' | 'bar'
>;
}
but if we go to the Model it's referencing for FinancingOption, found in the generated FinancingOption.ts file, the values generated here for the fields are in camelCase,
export interface FinancingOption {
/**
*
* @type {number}
* @memberof FinancingOption
*/
readonly id: number;
...
/**
*
* @type {Date}
* @memberof FinancingOption
*/
readonly createdAt: Date;
/**
*
* @type {Date}
* @memberof FinancingOption
*/
readonly updatedAt: Date;
...
The django model it's generating from is this (created_at and updated_at are inherited from the BaseModel):
from django.db import models
from ..utils.models import BaseModel
class FinancingOption(BaseModel):
...foo_parameters...
def __str__(self):
return self.title
class BaseModel(models.Model):
"""
Base model that includes default created / updated timestamps.
"""
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
And this is the serializer for this model:
from rest_framework import serializers
from .models import FinancingOption
class FinancingOptionSerializer(serializers.ModelSerializer):
class Meta:
model = FinancingOption
fields = [
"id",
"created_at",
"updated_at",
"foo",
"bar",
]
Because the interface FinancingOption is in camelCase but the interface FinanciersApiFinancingOptionCreateRequest is using Omit defined by snake_case variables, when I set up the parameters for the openapi request like so:
const params: FinanciersApiFinancingOptionCreateRequest = {
financingOption: {
financierFileAttachments: financierFileAttachments,
projectId: project.id,
team: team.id,
},
};
I end up getting this typescript error:
Type '{ financierFileAttachments: { [key: string]: string; }; projectId: number; team: number; }' is missing the following properties from type 'Omit<FinancingOption, "id" | "created_at" | "updated_at" | "foo" | "bar">': createdAt, updatedAtts(2739)
FinanciersApi.ts(31, 3): The expected type comes from property 'financingOption' which is declared here on type 'FinanciersApiFinancingOptionCreateRequest'
(property) FinanciersApiFinancingOptionCreateRequest.financingOption: Omit<FinancingOption, "id" | "created_at" | "updated_at" | "foo" | "bar">
openapi-generator version
7.8.0
OpenAPI declaration file content or url
Generation Details
typescript-fetch default settings
using a django stack with Django Rest Framework and DRF Spectacular
openapi-generator-cli generate \
-i http://localhost:8000/api/schema/ \
-g typescript-fetch \
-o ./assets/javascript/api-client/ \
Steps to reproduce
Create a model with the following:
from django.db import models
from ..utils.models import BaseModel
class FinancingOption(BaseModel):
...foo_parameters...
def __str__(self):
return self.title
class BaseModel(models.Model):
"""
Base model that includes default created / updated timestamps.
"""
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
And create this serializer as well in the same django app:
from rest_framework import serializers
from .models import FinancingOption
class FinancingOptionSerializer(serializers.ModelSerializer):
class Meta:
model = FinancingOption
fields = [
"id",
"created_at",
"updated_at",
"foo",
"bar",
]
Assuming you have the openapi-generator-cli installed in your terminal, run this command
openapi-generator-cli generate \
-i http://localhost:8000/api/schema/ \
-g typescript-fetch \
-o ./assets/javascript/api-client/ \
Navigate to the Api.ts file generated by the CLI, in my case it was called FinanciersApi.ts, and check to see if the Create class for this model (in my case it was interface FinanciersApiFinancingOptionCreateRequest) has the Omit class listing its parameters to be omitted as snake_case or camelCase.
Related issues/PRs
I searched around for this issue, and found things suggesting I try modifying the modelPropertyNaming config value, but that didn't change what I needed.
Suggest a fix
The clients generated need to list the interface values omitted as camelCase so they are consistent with the Model's themselves generated by the same api, which does list them as camelCase, because it needs to be camelCase to actually use the client calls, if snake_case were used it wouldn't actually be able to complete the calls.
Same issue in here