Closed raphaeloliveira closed 5 years ago
Expanding further, in my opinion the optional type should be mapped like this:
required |
nullable |
Type |
---|---|---|
false |
false |
Optional |
false |
true |
Optional |
true |
false |
Non Optional |
true |
true |
Optional |
This should also work seamlessly with decodeIfPresent, which returns nil
in the absence of the key or the key with value null
.
Hi @raphaeloliveira.
Yes some updates would be to be made to support this properly.
nullable
already exists on schema.metadata
at the moment. In Swagger 2.0 it was called x-nullable
though so the decoder needs to be updated here https://github.com/yonaskolb/SwagGen/blob/master/Sources/Swagger/Metadata.swift#L36 to something like
nullable = jsonDictionary.json(atKeyPath: "nullable") ?? jsonDictionary.json(atKeyPath: "x-nullable") ?? false
Then the optional
and optionalType
context properties would need to read from that in the way you suggested
!property.required || property.metadata.nullable
both here and here The parameter context could also be updated if the type is a schema.
If you could create a PR that would be great 😄👍
Hey @yonaskolb, here is the PR https://github.com/yonaskolb/SwagGen/pull/165.
I tested by installing my local branch swaggen version using make install
and generating code using a spec that used the nullable
attribute - all seemed to work fine.
Is there something else that needs to be done? Bumping version or something like that?
Thanks for that @raphaeloliveira. Once that is merged a release needs to be manually made. You could also point to master in your MintFile until then
Hey Yonas, I'm curious if SwagGen currently supports the nullable attribute as described in the Swagger docs. Apparently a
required
property withnullable: true
is not correctly mapped to an optional property on Swift.Example:
I started my investigation on
Model.stencil
:which led me to
which indeed doesn't seem to have a
nullable
field.I'm curious if the fix would be to update
ObjectSchema
initialiserand only add the property on the
requiredProperties
array ifnullable
isfalse
.Is that the right direction?