Referenced enums in swagger are not handled as enum
See original GitHub issueI have the following (simplified) json swagger :
{
"openapi": "3.0.0",
"info": {
"title": "Car API",
"description": "API Car",
"version": "1.0",
"contact": {}
},
"tags": [],
"servers": [],
"components": {
"schemas": {
"CarStatus": {
"type": "string",
"enum": [
"new",
"second_hand",
"broken"
]
},
"SwaggerCar": {
"type": "object",
"properties": {
"clientCarStatus": {
"$ref": "#/components/schemas/CarStatus"
}
},
}
}
},
"paths": {
"/cars": {
"get": {
"operationId": "CarsController_getCars",
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/SwaggerCars"
}
}
}
}
}
},
"tags": [
"contracts"
]
}
}
}
}
And I’m generating code with the following parameters :
codegen({
methodNameMode: 'operationId',
modelMode: 'class',
outputDir: `./src/Services/API`,
remoteUrl: `XXX/api-json`,
useClassTransformer: true,
});
The generating code contains things like :
/** */
@Expose()
@Type(() => CarStatus)
'clientCarStatus': CarStatus;
But CarStatus is an enum so it should not be transformed.
The issue only happens when enum are referenced.
I’m pretty sure tue issue comes from this part of the code (https://github.com/Manweill/swagger-axios-codegen/blob/69af03c84692ee14c963d7e3927fb64354986a06/src/componentsCodegen/propTrueType.ts#L17) :
if (v.$ref) {
// 是引用类型
result.propType = refClassName(v.$ref || v.allOf[0].$ref)
result.ref = result.propType
}
//是个数组
else if (v.items) {
[...]
}
// 是枚举 并且是字符串类型
else if (v.enum && v.type === 'string') {
result.isEnum = true
result.propType = getEnums(v.enum)
.map(item =>
isNaN(item)
? `'${item}'='${item}'`
: `'KEY_${item}'='${item}'`)
.join(',')
}
else if (v.enum) {
result.isType = true
result.propType = v.type === 'string' ? getEnums(v.enum).map(item => `'${item}'`).join('|') : v.enum.join('|')
}
➡️ if v is a ref it is never checked to be an enum.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:13 (12 by maintainers)
Top Results From Across the Web
Enums - Swagger
Enums. You can use the enum keyword to specify possible values of a request ... You can define reusable enums in the global...
Read more >Swagger UI Web Api documentation Present enums as strings?
I want to be able to submit POST actions and put enums according to their string value without having to look at the...
Read more >Enums are not shown · Issue #6906 · swagger-api ... - GitHub
Go to online editor; Paste YAML; Expand enums. Expected behavior. When expanded, an enum should show the list of possible values. Screenshots.
Read more >Description of the Enumeration Members in Swashbuckle
But it does not show the descriptions of the enum members. Here, I'll show how to add them. Swagger is a great thing!...
Read more >Enum value used as example appears twice in combo box
Hello, When declaring in my OpenAPI spec a field such as: ``` "carColor":{"type":"string","example":"WHITE","description":"Color of the car","enum":["WHITE" ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Funny that you created an issue today, my team faced the exact problem a few days ago while enabling the
useClassTransformerconfig to properly cast dates in the generated client !I only listed two because we use only two in our app. What i wanted to show is a way of how to get the dates out of the json. You could also check for the key if it contains “day” or “date”, you could convert each string value to a Date and check if it a valid date.
I don’t know if i got the second point correctly but i try to answer it the way i understood it. The reviver function has access to the key and the value of the field. So one could also use the key as an indicator if the value is a Date by checking common names. So a number can also be parsed. Or if the name is not enough you could also parse the date and check if it is valid. This way you could also convert numbers if numbers are returned by the backend.
In our case we send all dates as string in UTC format so we know this reviver function will work in 100% of the cases.
In my case all da