Skip to content

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

@chgeuer

Description

@chgeuer
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions