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

Arrays handling seems bugged #155

Open
krzybob opened this issue Sep 16, 2021 · 4 comments
Open

Arrays handling seems bugged #155

krzybob opened this issue Sep 16, 2021 · 4 comments

Comments

@krzybob
Copy link

krzybob commented Sep 16, 2021

I found a bug when generating api for a service responsing with an array of objects. Generated code doesn't point to proper class definition (TestObject in the example below replaced with Any?) despite it was properly generated.

Input:

{
  "swagger": "2.0",
  "info": {
    "version": "1.0",
    "title": "test-swagger",
    "description": "https://test-swagger.com/"
  },
  "schemes": [
    "https",
    "http"
  ],
  "produces": [
    "application/json"
  ],
  "consumes": [
    "application/json"
  ],
  "paths": {
    "/api/test/swagger": {
      "get": {
        "tags": [
          "test-swagger"
        ],
        "summary": "Test swagger.",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "description": "Uniqe id.",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "properties": {
                "cities": {
                  "type": "array",
                  "items": {
                    "$ref": "#/definitions/TestObject"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "TestObject": {
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  }
}

Output:

package com.test.package.apis

import com.test.package.models.Any?
import retrofit2.http.GET
import retrofit2.http.Headers

@JvmSuppressWildcards
interface TestSwaggerApi {
    /**
     * Test swagger.
     * The endpoint is owned by defaultname service owner
     * @param id Uniqe id. (required)
     */
    @Headers(
        "Content-Type: application/json"
    )
    @GET("/api/test/swagger")
    suspend fun apiTestSwaggerGet(
        @retrofit2.http.Query("id") id: String
    ): Map<String, Any?>
}
package com.test.package.models

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
 * @property name
 */
@JsonClass(generateAdapter = true)
data class TestObject(
    @Json(name = "name") @field:Json(name = "name") var name: String? = null
)
@cortinico
Copy link
Collaborator

The specs looks correct on my end. @macisamuele are you able to confirm?
If so this could be an actual bug in the generator.

@krzybob Which version of the library are you using?

@krzybob
Copy link
Author

krzybob commented Sep 17, 2021

@cortinico 1.4.1

@macisamuele
Copy link
Collaborator

@cortinico, @krzybob I'll be checking this in the next days.
I do have a suspect that the issue is caused by the handling of schemas without type property, but I need to verify it.

@macisamuele
Copy link
Collaborator

swagger-gradle-codegen assumes a JSON Object in the response (due to moshi deserialisation). A JSON Object is typed as Map<String, Any?>.
I'm not sure if this is fully advertised or even if this is still needed.

Focusing on API response JSON Schema

properties:
  cities:
    type: array
    items:
      $ref: '#/definitions/TestObject'

it does not even requires to be an object as [1,2,3] would be technically a valid schema.

I would suggest to add type: object into the response schema and check if the generated code is more in line with the expectations[1].
If so I think that we have a temporary workaround as well as a mire directive approach on dealing with the specific case.

[1] Unfortunately, I don't have a laptop with me, hence it is complex for me to verify the code within the next 2 weeks.

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

No branches or pull requests

3 participants