OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.62k stars 6.29k forks source link

[BUG][Elixir] --name-mappings is not applied to property names #18661

Open valpackett opened 1 month ago

valpackett commented 1 month ago
Description

The Elixir generator does not apply --name-mappings which are e.g. necessary to work around a field called _.

openapi-generator version

0e05cf26d94

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: 1.0.0
  title: test
paths:
  /test:
    get:
      summary: sample
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  _:
                    type: string
Generation Details
java -jar /home/val/.m2/repository/org/openapitools/openapi-generator-cli/7.6.0-SNAPSHOT/openapi-generator-cli-7.6.0-SNAPSHOT.jar generate -i spec.yaml -o undersc/ -g elixir --name-mappings _=underscore
Steps to reproduce

generate, see this:

  @derive Jason.Encoder
  defstruct [
    :_
  ]

  @type t :: %__MODULE__{
    :_ => String.t | nil
  }
==> test
Compiling 5 files (.ex)
    error: invalid use of _. _ can only be used inside patterns to ignore values and cannot be used in expressions. Make sure you are inside a pattern or change it accordingly
    │
 10 │   defstruct [
    │   ^^^^^^^^^^^
    │
    └─ lib/test/model/_test_get_200_response.ex:10: Jason.Encoder.Test.Model.TestGet200Response.encode/2
Related issues/PRs

18398 for a default _ mapping in Python

Suggest a fix

mrmstn commented 4 weeks ago

Unfortunately, the generated code currently doesn't support any type of mapping and relies heavily on the variable names being directly mappable 1:1. It would indeed be great to implement such logic to accommodate reserved and unusual words.

If you have some spare time, a PR from you to introduce this logic would be highly appreciated!