Skip to content

Add .t in spec generation of Elixir structs #8159

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

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generators/elixir.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ These options may be applied as additional-properties (cli) or configOptions (pl
<li>Atom</li>
<li>Boolean</li>
<li>DateTime</li>
<li>Decimal</li>
<li>Float</li>
<li>Integer</li>
<li>List</li>
<li>Map</li>
<li>PID</li>
<li>String</li>
<li>Tuple</li>
<li>map()</li>
</ul>

## RESERVED WORDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,16 @@ public ElixirClientCodegen() {
Arrays.asList(
"Integer",
"Float",
"Decimal",
"Boolean",
"String",
"List",
"Atom",
"Map",
"Tuple",
"PID",
"DateTime"
"DateTime",
"map()" // This is a workaround, since the DefaultCodeGen uses our elixir TypeSpec datetype to evaluate the primitive
)
);

Expand Down Expand Up @@ -510,8 +512,7 @@ public String getTypeDeclaration(Schema p) {
} else if (ModelUtils.isDateTimeSchema(p)) {
return "DateTime.t";
} else if (ModelUtils.isObjectSchema(p)) {
// TODO How to map it?
return super.getTypeDeclaration(p);
return "map()";
} else if (ModelUtils.isIntegerSchema(p)) {
return "integer()";
} else if (ModelUtils.isNumberSchema(p)) {
Expand All @@ -520,9 +521,8 @@ public String getTypeDeclaration(Schema p) {
return "String.t";
} else if (ModelUtils.isBooleanSchema(p)) {
return "boolean()";
} else if (!StringUtils.isEmpty(p.get$ref())) { // model
// How to map it?
return super.getTypeDeclaration(p);
} else if (!StringUtils.isEmpty(p.get$ref())) {
return this.moduleName + ".Model." + super.getTypeDeclaration(p) + ".t";
} else if (ModelUtils.isFileSchema(p)) {
return "String.t";
} else if (ModelUtils.isStringSchema(p)) {
Expand Down Expand Up @@ -593,12 +593,12 @@ public ExtendedCodegenResponse(CodegenResponse o) {
this.isDefinedDefault = (this.code.equals("0") || this.code.equals("default"));
}

public String codeMappingKey(){
if(this.isDefinedDefault) {
public String codeMappingKey() {
if (this.isDefinedDefault) {
return ":default";
}

if(code.matches("^\\d{3}$")){
if (code.matches("^\\d{3}$")) {
return code;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ defmodule {{moduleName}}.Api.{{classname}} do
{{/optionalParams}}
## Returns

{:ok, {{#isArray}}[%{{&returnBaseType}}{}, ...]{{/isArray}}{{#isMap}}%{}{{/isMap}}{{^returnType}}%{}{{/returnType}}{{#returnSimpleType}}%{{#returnType}}{{#isMap}}{{/isMap}}{{{moduleName}}}.Model.{{{returnType}}}{{/returnType}}{}{{/returnSimpleType}}} on success
{:error, info} on failure
{:ok, {{#isArray}}[%{{&returnBaseType}}{}, ...]{{/isArray}}{{#isMap}}%{}{{/isMap}}{{^returnType}}nil{{/returnType}}{{#returnSimpleType}}{{#returnType}}{{#isMap}}{{/isMap}}{{{returnType}}}{{/returnType}}{{/returnSimpleType}}} on success
{:error, Tesla.Env.t} on failure
"""
{{{typespec}}}
def {{{operationId}}}(connection, {{#requiredParams}}{{#underscored}}{{{paramName}}}{{/underscored}}, {{/requiredParams}}{{^hasOptionalParams}}_{{/hasOptionalParams}}opts \\ []) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ defimpl Poison.Decoder, for: {{&moduleName}}.Model.{{&classname}} do
value
{{#vars}}
{{^isPrimitiveType}}
{{#datatype}}|> deserialize(:"{{&baseName}}", {{#isArray}}:list, {{&moduleName}}.Model.{{{items.datatype}}}{{/isArray}}{{#isMap}}:map, {{&moduleName}}.Model.{{{items.datatype}}}{{/isMap}}{{#isDate}}:date, nil{{/isDate}}{{#isDateTime}}:date, nil{{/isDateTime}}{{^isDate}}{{^isDateTime}}{{^isMap}}{{^isArray}}:struct, {{moduleName}}.Model.{{dataType}}{{/isArray}}{{/isMap}}{{/isDateTime}}{{/isDate}}, options)
{{/datatype}}
{{#baseType}}|> deserialize(:"{{&baseName}}", {{#isArray}}:list, {{&moduleName}}.Model.{{{items.baseType}}}{{/isArray}}{{#isMap}}:map, {{&moduleName}}.Model.{{{items.baseType}}}{{/isMap}}{{#isDate}}:date, nil{{/isDate}}{{#isDateTime}}:date, nil{{/isDateTime}}{{^isDate}}{{^isDateTime}}{{^isMap}}{{^isArray}}:struct, {{moduleName}}.Model.{{baseType}}{{/isArray}}{{/isMap}}{{/isDateTime}}{{/isDate}}, options)
{{/baseType}}
{{/isPrimitiveType}}
{{/vars}}
{{/hasComplexVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule OpenapiPetstore.Api.AnotherFake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %OpenapiPetstore.Model.Client{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.Client.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec call_123_test_special_tags(Tesla.Env.client, OpenapiPetstore.Model.Client.t, keyword()) :: {:ok, OpenapiPetstore.Model.Client.t} | {:error, Tesla.Env.t}
def call_123_test_special_tags(connection, body, _opts \\ []) do
Expand Down
56 changes: 28 additions & 28 deletions samples/client/petstore/elixir/lib/openapi_petstore/api/fake.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec create_xml_item(Tesla.Env.client, OpenapiPetstore.Model.XmlItem.t, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def create_xml_item(connection, xml_item, _opts \\ []) do
Expand All @@ -48,8 +48,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :body (boolean()): Input boolean as post body
## Returns

{:ok, %OpenapiPetstore.Model.boolean(){}} on success
{:error, info} on failure
{:ok, boolean()} on success
{:error, Tesla.Env.t} on failure
"""
@spec fake_outer_boolean_serialize(Tesla.Env.client, keyword()) :: {:ok, Boolean.t} | {:error, Tesla.Env.t}
def fake_outer_boolean_serialize(connection, opts \\ []) do
Expand Down Expand Up @@ -77,8 +77,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :body (OuterComposite): Input composite as post body
## Returns

{:ok, %OpenapiPetstore.Model.OuterComposite{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.OuterComposite.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec fake_outer_composite_serialize(Tesla.Env.client, keyword()) :: {:ok, OpenapiPetstore.Model.OuterComposite.t} | {:error, Tesla.Env.t}
def fake_outer_composite_serialize(connection, opts \\ []) do
Expand Down Expand Up @@ -106,8 +106,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :body (float()): Input number as post body
## Returns

{:ok, %OpenapiPetstore.Model.float(){}} on success
{:error, info} on failure
{:ok, float()} on success
{:error, Tesla.Env.t} on failure
"""
@spec fake_outer_number_serialize(Tesla.Env.client, keyword()) :: {:ok, Float.t} | {:error, Tesla.Env.t}
def fake_outer_number_serialize(connection, opts \\ []) do
Expand Down Expand Up @@ -135,8 +135,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :body (String.t): Input string as post body
## Returns

{:ok, %OpenapiPetstore.Model.String.t{}} on success
{:error, info} on failure
{:ok, String.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec fake_outer_string_serialize(Tesla.Env.client, keyword()) :: {:ok, String.t} | {:error, Tesla.Env.t}
def fake_outer_string_serialize(connection, opts \\ []) do
Expand Down Expand Up @@ -164,8 +164,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_body_with_file_schema(Tesla.Env.client, OpenapiPetstore.Model.FileSchemaTestClass.t, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_body_with_file_schema(connection, body, _opts \\ []) do
Expand All @@ -190,8 +190,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_body_with_query_params(Tesla.Env.client, String.t, OpenapiPetstore.Model.User.t, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_body_with_query_params(connection, query, body, _opts \\ []) do
Expand All @@ -218,8 +218,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %OpenapiPetstore.Model.Client{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.Client.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_client_model(Tesla.Env.client, OpenapiPetstore.Model.Client.t, keyword()) :: {:ok, OpenapiPetstore.Model.Client.t} | {:error, Tesla.Env.t}
def test_client_model(connection, body, _opts \\ []) do
Expand Down Expand Up @@ -258,8 +258,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :callback (String.t): None
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_endpoint_parameters(Tesla.Env.client, float(), float(), String.t, binary(), keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_endpoint_parameters(connection, number, double, pattern_without_delimiter, byte, opts \\ []) do
Expand Down Expand Up @@ -309,8 +309,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :enum_form_string (String.t): Form parameter enum test (string)
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_enum_parameters(Tesla.Env.client, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_enum_parameters(connection, opts \\ []) do
Expand Down Expand Up @@ -352,8 +352,8 @@ defmodule OpenapiPetstore.Api.Fake do
- :int64_group (integer()): Integer in group parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_group_parameters(Tesla.Env.client, integer(), boolean(), integer(), keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_group_parameters(connection, required_string_group, required_boolean_group, required_int64_group, opts \\ []) do
Expand Down Expand Up @@ -386,8 +386,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_inline_additional_properties(Tesla.Env.client, %{optional(String.t) => String.t}, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_inline_additional_properties(connection, param, _opts \\ []) do
Expand All @@ -413,8 +413,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_json_form_data(Tesla.Env.client, String.t, String.t, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_json_form_data(connection, param, param2, _opts \\ []) do
Expand Down Expand Up @@ -444,8 +444,8 @@ defmodule OpenapiPetstore.Api.Fake do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_query_parameter_collection_format(Tesla.Env.client, list(String.t), list(String.t), list(String.t), list(String.t), list(String.t), keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def test_query_parameter_collection_format(connection, pipe, ioutil, http, url, context, _opts \\ []) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule OpenapiPetstore.Api.FakeClassnameTags123 do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %OpenapiPetstore.Model.Client{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.Client.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec test_classname(Tesla.Env.client, OpenapiPetstore.Model.Client.t, keyword()) :: {:ok, OpenapiPetstore.Model.Client.t} | {:error, Tesla.Env.t}
def test_classname(connection, body, _opts \\ []) do
Expand Down
32 changes: 16 additions & 16 deletions samples/client/petstore/elixir/lib/openapi_petstore/api/pet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ defmodule OpenapiPetstore.Api.Pet do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec add_pet(Tesla.Env.client, OpenapiPetstore.Model.Pet.t, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def add_pet(connection, body, _opts \\ []) do
Expand All @@ -49,8 +49,8 @@ defmodule OpenapiPetstore.Api.Pet do
- :api_key (String.t):
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec delete_pet(Tesla.Env.client, integer(), keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def delete_pet(connection, pet_id, opts \\ []) do
Expand Down Expand Up @@ -81,7 +81,7 @@ defmodule OpenapiPetstore.Api.Pet do
## Returns

{:ok, [%Pet{}, ...]} on success
{:error, info} on failure
{:error, Tesla.Env.t} on failure
"""
@spec find_pets_by_status(Tesla.Env.client, list(String.t), keyword()) :: {:ok, nil} | {:ok, list(OpenapiPetstore.Model.Pet.t)} | {:error, Tesla.Env.t}
def find_pets_by_status(connection, status, _opts \\ []) do
Expand Down Expand Up @@ -109,7 +109,7 @@ defmodule OpenapiPetstore.Api.Pet do
## Returns

{:ok, [%Pet{}, ...]} on success
{:error, info} on failure
{:error, Tesla.Env.t} on failure
"""
@spec find_pets_by_tags(Tesla.Env.client, list(String.t), keyword()) :: {:ok, nil} | {:ok, list(OpenapiPetstore.Model.Pet.t)} | {:error, Tesla.Env.t}
def find_pets_by_tags(connection, tags, _opts \\ []) do
Expand All @@ -136,8 +136,8 @@ defmodule OpenapiPetstore.Api.Pet do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %OpenapiPetstore.Model.Pet{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.Pet.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec get_pet_by_id(Tesla.Env.client, integer(), keyword()) :: {:ok, nil} | {:ok, OpenapiPetstore.Model.Pet.t} | {:error, Tesla.Env.t}
def get_pet_by_id(connection, pet_id, _opts \\ []) do
Expand All @@ -163,8 +163,8 @@ defmodule OpenapiPetstore.Api.Pet do
- opts (KeywordList): [optional] Optional parameters
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec update_pet(Tesla.Env.client, OpenapiPetstore.Model.Pet.t, keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def update_pet(connection, body, _opts \\ []) do
Expand Down Expand Up @@ -194,8 +194,8 @@ defmodule OpenapiPetstore.Api.Pet do
- :status (String.t): Updated status of the pet
## Returns

{:ok, %{}} on success
{:error, info} on failure
{:ok, nil} on success
{:error, Tesla.Env.t} on failure
"""
@spec update_pet_with_form(Tesla.Env.client, integer(), keyword()) :: {:ok, nil} | {:error, Tesla.Env.t}
def update_pet_with_form(connection, pet_id, opts \\ []) do
Expand Down Expand Up @@ -226,8 +226,8 @@ defmodule OpenapiPetstore.Api.Pet do
- :file (String.t): file to upload
## Returns

{:ok, %OpenapiPetstore.Model.ApiResponse{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.ApiResponse.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec upload_file(Tesla.Env.client, integer(), keyword()) :: {:ok, OpenapiPetstore.Model.ApiResponse.t} | {:error, Tesla.Env.t}
def upload_file(connection, pet_id, opts \\ []) do
Expand Down Expand Up @@ -258,8 +258,8 @@ defmodule OpenapiPetstore.Api.Pet do
- :additional_metadata (String.t): Additional data to pass to server
## Returns

{:ok, %OpenapiPetstore.Model.ApiResponse{}} on success
{:error, info} on failure
{:ok, OpenapiPetstore.Model.ApiResponse.t} on success
{:error, Tesla.Env.t} on failure
"""
@spec upload_file_with_required_file(Tesla.Env.client, integer(), String.t, keyword()) :: {:ok, OpenapiPetstore.Model.ApiResponse.t} | {:error, Tesla.Env.t}
def upload_file_with_required_file(connection, pet_id, required_file, opts \\ []) do
Expand Down
Loading