swagger-api / swagger-api/swagger-codegen

[Elixir] Bug in 2.3.1. Generating multi-part body instead of direct JSON content

Open
#8,138 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The generated Elixir code from a Swagger definition seems to be wrong, but I'm not sure.

I try to generate an Elixir SDK for the Azure Resource Management API. I wrote a generator app, which uses uses swagger-codegen-cli-2.3.1.jar to generate Elixir code.

The swagger spec document says "in": "body", and the API expects JSON structure directly in the body, but the Elixir code generates a multi-part body.

      "put": {
        "operationId": "Deployments_CreateOrUpdate",
        "summary": "Deploys resources to a resource group.",
        "parameters": [
          ...
          {
            "name": "parameters",
            "in": "body",
            "required": true,
            "schema": { "$ref": "#/definitions/Deployment" },
            "description": "Additional parameters supplied to the operation."
          },

The Elixir swagger generator creates this code

  @spec deployments_create_or_update(Tesla.Env.client, String.t, String.t, Microsoft.Azure.Management.Resources.Model.Deployment.t, String.t, String.t, keyword()) :: {:ok, Microsoft.Azure.Management.Resources.Model.DeploymentExtended.t} | {:error, Tesla.Env.t}
  def deployments_create_or_update(connection, resource_group_name, deployment_name, parameters, api_version, subscription_id, _opts \\ []) do
    %{}
    |> method(:put)
    |> url("/subscriptions/#{subscription_id}/resourcegroups/#{resource_group_name}/providers/Microsoft.Resources/deployments/#{deployment_name}")
    |> add_param(:body, :"parameters", parameters)
    |> add_param(:query, :"api-version", api_version)

The parameter JSON is added as |> add_param(:body, :"parameters", parameters), which results in

 body: %Tesla.Multipart{
    boundary: "6R3XOsKKCMYMatbOce3P7yRHKD6Zmm",
    content_type_params: [],
    parts: [
      %Tesla.Multipart.Part{
        dispositions: [name: :parameters],
        headers: ["Content-Type": "application/json"],
        body: "{\"properties\":{\"template\":{\"a\":\"b\"},\"parameters\":{\"a\":\"b\"},\"mode\":\"Incremental\"}}"
      }
    ]
  },

instead of

  body: %{
    properties: %{ 
        parameters: %{a: "b"}, 
        template: %{a: "b"}, 
        mode: "Incremental" 
    }
  },

Shouldn't the generated SDK code be add_param(:body, :body, parameters) instead of add_param(:body, :"parameters", parameters)?

When I check the Swagger 2 Spec, it says that in: body means that the payload is directly in the body, not in a Multi-part message.

Swagger-codegen version

I am using version 2.3.1

Swagger declaration file content or url

The Swagger definition is https://github.com/Azure/azure-rest-api-specs/blob/master/specification/resources/resource-manager/Microsoft.Resources/stable/2018-02-01/resources.json#L155-L162

      "put": {
        "operationId": "Deployments_CreateOrUpdate",
        "summary": "Deploys resources to a resource group.",
        "parameters": [
          ...
          {
            "name": "parameters",
            "in": "body",
            "required": true,
            "schema": { "$ref": "#/definitions/Deployment" },
            "description": "Additional parameters supplied to the operation."
          },
Command line used for generation
#!/bin/bash

curl -O http://central.maven.org/maven2/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar

cat > Microsoft.Azure.Management.Resources.json <<-EOF
    { 
        "packageName": "azure", 
        "invokerPackage": "Microsoft.Azure.Management.Resources" 
    }
EOF

java \
   -jar swagger-codegen-cli-2.3.1.jar \
   generate \
   -l elixir \
   -i https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/resources/resource-manager/Microsoft.Resources/stable/2018-02-01/resources.json \
   -o clients/Microsoft.Azure.Management \
   -c ./Microsoft.Azure.Management.Resources.json

sed -n 91,100p clients/Microsoft.Azure.Management/lib/microsoft/azure/management/resources/api/deployments.ex
Consumer-side fix

When I replace all occurrences of add_param(:body, :"whatever-here", parameters) in the .ex-files, my SDK works.

#!/bin/bash
find . -type f -name "*.ex" -exec \
   sed -i'' -e 's/add_param(:body, :"[^"]*", /add_param(:body, :body, /g' {} +

/cc @niku

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the posted swagger-codegen 2.3.1 command with the linked Azure definition and inspect the generated deployments.ex file. Trace how the Elixir generator handles the Swagger in: body parameter; done means the generated call sends the parameter as the direct JSON body rather than a Tesla.Multipart body.

Written by the indexing model from the issue text.

Assessment

Tech stack
elixir, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.