When generating APIs with Date-Time parameters, the generated service returns the error "key may not be null if value is Date".
This was introduced by 4.2.3. I'm having no issues running 4.0.1.
"parameters": [{
"name": "date",
"in": "query",
"type": "string",
"format": "date-time"
}
]
openapi-generator generate -g typescript-angular -i [swaggerUri] -o [outputPath] -c scripts/open-api-generator.conf.json
Possibly related to https://github.com/OpenAPITools/openapi-generator/issues/4404
The problem is addToHttpParams and addToHttpParamsRecursive.
For values with 'typeof values ==="object"', addToHttpParamsRecursive is called without the 'key' property. But if 'value instanceof Date', a key is required; otherwise the error is raised.
馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
this should be fixed with https://github.com/OpenAPITools/openapi-generator/issues/5174
@macjohnny I think this is unrelated, as PR 5197 just adds a null check. In my case, value is not null, but a valid date.
@mcpummec would you like to file a PR to fix this?
@mcpummec would you like to file a PR to fix this?
Sure, but unfortunately I can't push a feature branch (403).
just fork the repository, push a branch there, and then file a PR
thanks @macjohnny for your support & quick respone
@mcpummec a late question: what do you do with Dates coming as a part of response models? Currently, openapi-generator doesn't de-serialize date-formatted strings into Date objects.
A common workaround to the problem is to add --typeMapping DateTime=string param to the generator. Which effectively also eliminates the issue you described.
Are you doing this de-serialization yourself in the code that consumes the generated apis?
@amakhrov here it is only about the query parameters, not model parameters. Query parameters are handled in a different way
@amakhrov tbh for me date-times within response models are deserialized just fine.
swagger definition of
"ResponseObject": {
"type": "object",
"properties": {
"date": {
"format": "date-time",
"type": "string"
}
}
}
results in something like
export interface ResponseObject{
date ? : Date;
}
using the generator command posted above.
@mcpummec yep, that's what I mean. The generated type is Date. But the value coming over http is just string, and the generated code doesn't have explicit conversion for that. It means that type Date is simply incorrect here.
That's why users usually add custom typemapping (passed as a cli argument to the generator).
And with that custom mapping your particular issue with a request param of type Date would also self-resolve. It will be just strings and not Date any more
Having same issue. Moreover the date string value has stripped out the time part, any reason for that?
} else if (value instanceof Date) {
if (key != null) {
httpParams = httpParams.append(key,
(value as Date).toISOString().substr(0, 10)); // <------------ HERE
} else {
throw Error("key may not be null if value is Date");
}
} else {
@skorunka this happens because it is a date and not date-time, so the time part is removed.
@macjohnny Hi, the API spes says it is a datetime:
{
"in" : "query",
"name" : "dateTimeParameter",
"required" : false,
"schema" : {
"format" : "date-time",
"type" : "string"
}
},
This change actually breakes it https://github.com/OpenAPITools/openapi-generator/pull/4337/commits/96bf86ffb0174de7df535bf98799d176db65c793
I just wonder, how to pass the date-time(including the time part) as a querystring parameter, after this change? Cheers.
@skorunka you are right, the "old" behavior was to use type Date for date-time values, and string for date values. Therefore this is wrong.
However, the type-mapping of date-time to Date was removed in https://github.com/OpenAPITools/openapi-generator/pull/5314, as this was Bug, since there no date serialization / deserialization logic is implemented in the typescript-angular generator for the model or header parameters, only for the query parameters.
@macjohnny I see. What if you need a date-time parameter in the querystring? Any chance? Thank you.
@skorunka the easies way would be to convert it to a string first, e.g. with .toISOString(), in your consumer code.
Ok, thank you.
Most helpful comment
thanks @macjohnny for your support & quick respone