-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[elixir] Typespec - allow null on optional struct-attributes #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@mrmstn thanks for the PR. Shall we also use the Ref: |
I think this would be a good idea - I kind of missed the existance of |
I also added the nullable type to the musache file. Based on the following swagger 2.0 defintion definitions:
Entity:
type: object
properties:
requiredDate:
type: string
format: date
x-nullable: false
notRequiredDate:
type: string
format: date
x-nullable: false
nullableRequiredDate:
type: string
format: date
x-nullable: true
nullableNotRequiredDate:
type: string
format: date
x-nullable: true
required:
- requiredDate
- nullableRequiredDate this PR will result to the following results defmodule SwaggerPetstore.Model.Entity do
@moduledoc """
"""
@derive [Poison.Encoder]
defstruct [
:"requiredDate",
:"notRequiredDate",
:"nullableRequiredDate",
:"nullableNotRequiredDate"
]
@type t :: %__MODULE__{
:"requiredDate" => Date.t,
:"notRequiredDate" => Date.t | nil,
:"nullableRequiredDate" => Date.t | nil,
:"nullableNotRequiredDate" => Date.t | nil
}
end As you can see, even if you didn't allow a non required attribute to be null, the typespec will mark the attribute as possible null. This is given to the fact that elixir will initiate all attributes in a struct as null and therefore a "optional" attribute in a struct can only be null and not "not present" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
…Tools#1514) * Typespec - allow null on optional parameters * Run Petstore for elixir * considers 'nullable' in model template, fixes 'isRequired'
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
and./bin/security/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
.master
,3.4.x
,4.0.x
. Default:master
.Description of the PR
This PR allows null-values for struct-attributes which are not marked as required. This is very important since the compiler would mark this as a error since a null value in a struct would break the typespec contract.
With this small typespec fix, non-required and nullable attributes will be markes as possible nil values for the given model