Skip to content
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

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

Open
chgeuer opened this issue May 4, 2018 · 1 comment · May be fixed by #8391
Open

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

chgeuer opened this issue May 4, 2018 · 1 comment · May be fixed by #8391

Comments

@chgeuer
Copy link

chgeuer commented May 4, 2018

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

@jdalberg
Copy link

I just debugged myself to the same thing, so i agree on the :body, :body part :)

chgeuer added a commit to chgeuer/swagger-api--swagger-codegen that referenced this issue Jul 3, 2018
When JSON payload needs to be in the body (`"in": "body"`), it must not generate a multi-part body, but embed directly in HTTP body.
wing328 pushed a commit to OpenAPITools/openapi-generator that referenced this issue Nov 20, 2018
…en#8138

When JSON payload needs to be in the body (`"in": "body"`), it must not generate a multi-part body, but embed directly in HTTP body.
wing328 added a commit to OpenAPITools/openapi-generator that referenced this issue Nov 28, 2018
* Fix swagger-api/swagger-codegen/issues/8138 swagger-api/swagger-codegen#8138

When JSON payload needs to be in the body (`"in": "body"`), it must not generate a multi-part body, but embed directly in HTTP body.

* update elixir samples

* update elixir client samples

* remove double quote
A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this issue Feb 27, 2019
* Fix swagger-api/swagger-codegen/issues/8138 swagger-api/swagger-codegen#8138

When JSON payload needs to be in the body (`"in": "body"`), it must not generate a multi-part body, but embed directly in HTTP body.

* update elixir samples

* update elixir client samples

* remove double quote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants