Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
185 lines (167 sloc) 5.73 KB

In this page you can find some samples of generated code using swagger-gradle-codegen for every platform:

kotlin platform

The following specs:

{
    "definitions": {
        "Category": {
            "description": "A Category used to group pets",
            "properties": {
                "id": {
                    "description": "Unique ID of the Category",
                    "format": "int64",
                    "type": "integer"
                },
                "name": {
                    "description": "Name of this category",
                    "type": "string"
                }
            },
            "type": "object"
        },
        "Pet": {
            "description": "Represents a specific Pet in the store",
            "properties": {
                "category": {
                    "$ref": "#/definitions/Category",
                    "description": "Optional category of the pet"
                },
                "id": {
                    "description": "Unique ID of this Pet",
                    "format": "int64",
                    "type": "integer"
                },
                "name": {
                    "description": "Name of this specific pet",
                    "type": "string"
                },
                "photoUrls": {
                    "description": "Photo URls for this Pet on the bucket",
                    "items": {
                        "type": "string"
                    },
                    "type": "array"
                },
                "tags": {
                    "description": "Pet status in the store",
                    "items": {
                        "$ref": "#/definitions/Tag"
                    },
                    "type": "array"
                }
            },
            "required": [
                "name",
                "photoUrls"
            ],
            "type": "object"
        },
        "Tag": {
            "description": "A Tag used to group entities",
            "properties": {
                "id": {
                    "description": "Unique ID of the Tag",
                    "format": "int64",
                    "type": "integer"
                },
                "name": {
                    "description": "Name of this Tag",
                    "type": "string"
                }
            },
            "type": "object"
        }
    },
    "info": {
        "description": "This is a simplified version of the sample server Petstore server.",
        "title": "Swagger Petstore",
        "version": "1.0.0"
    },
    "paths": {
        "/pet/{petId}": {
            "get": {
                "description": "Returns a single pet",
                "operationId": "getPetById",
                "parameters": [
                    {
                        "description": "ID of pet to return",
                        "format": "int64",
                        "in": "path",
                        "name": "petId",
                        "required": true,
                        "type": "integer"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/Pet"
                        }
                    }
                },
                "summary": "Find pet by ID"
            }
        }
    },
    "swagger": "2.0"
}

Will generate the following Retrofit Interfaces and Kotlin Data Classes:

DefaultApi.kt

DefaultApi.kt will contain a Retrofit interface with all the endpoints, in this case just one:

/**
 * NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: Swagger Petstore
 *
 * More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
 */

package com.yelp.codegen.generatecodesamples.apis

import com.yelp.codegen.generatecodesamples.models.Pet
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Headers

@JvmSuppressWildcards
interface DefaultApi {
    /**
     * Find pet by ID
     * Returns a single pet
     * The endpoint is owned by generatecodesamples service owner
     * @param petId ID of pet to return (required)
     */
    @Headers(
            "X-Operation-ID: getPetById"
    )
    @GET("/pet/{petId}")
    fun getPetById(
        @retrofit2.http.Path("petId") petId: Long
    ): Single<Pet>
}

Pets.kt

Pets.kt will contain a Kotlin Data class that will be used as response type for the /pet/{petID} endpoint.

Other classes are generated as well (Category & Tag). They're omitted for brevity but they're available inside this folder.

/**
 * NOTE: This class is auto generated by the Swagger Gradle Codegen for the following API: Swagger Petstore
 *
 * More info on this tool is available on https://github.com/Yelp/swagger-gradle-codegen
 */

package com.yelp.codegen.generatecodesamples.models

import com.squareup.moshi.Json

/**
 * Represents a specific Pet in the store
 * @property id Unique ID of this Pet
 * @property category Optional category of the pet
 * @property name Name of this specific pet
 * @property photoUrls Photo URls for this Pet on the bucket
 * @property tags Pet status in the store
 */
data class Pet(
    @Json(name = "name") @field:Json(name = "name") var name: String,
    @Json(name = "photoUrls") @field:Json(name = "photoUrls") var photoUrls: List<String>,
    @Json(name = "id") @field:Json(name = "id") var id: Long? = null,
    @Json(name = "category") @field:Json(name = "category") var category: Category? = null,
    @Json(name = "tags") @field:Json(name = "tags") var tags: List<Tag>? = null
)
You can’t perform that action at this time.