swagger-api / swagger-api/swagger-codegen

[kotlin-server] Paths.kt does not contain post-operations

Open
#8,586 0 comments 0 reactions 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

When generating a kotlin-server, the generated Paths.kt does not contain locations for post-operations.

Swagger-codegen version

2.4.0-SNAPSHOT (9544f725cd7d2da09c8da42ac5bc84c0bb4c05af)

Swagger declaration file content or url
swagger: "2.0"
host: "0.0.0.0"
info:
  title: "Kotlin Demo"
  description: "This is a Kotlin Swagger Demo"
  version: "0.0.1"
basePath: "/v1"
paths:
  /user/{userId}:
    get:
      operationId: "getUser"
      produces:
      - "application/json"
      responses:
        200:
          description: "Success."
          schema:
            $ref: '#/definitions/IdentifiableUser'
      parameters:
      - name: "userId"
        in: "path"
        required: true
        type: "integer"
  /user:
    post:
      operationId: "postUser"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        required: true
        schema:
          $ref: "#/definitions/User"
      responses:
        202:
          description: "Success"
        400:
          description: "Invalid User."
definitions:
  Identifiable:
    type: "object"
    required:
    - "id"
    properties:
      id:
        type: "integer"
  User:
    type: "object"
    required:
    - "name"
    - "type"
    properties:
      name:
        type: "string"
      type:
        type: "string"
        enum: ["user", "moderator"]
      info:
        type: "string"
  IdentifiableUser:
    type: "object"
    allOf:
    - $ref: "#/definitions/Identifiable"
    - $ref: "#/definitions/User"

Results in:

/**
* Kotlin Demo
* This is a Kotlin Swagger Demo
*
* OpenAPI spec version: 0.0.1
* 
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/
package de.gfelbing.kotlin.swagger

import io.ktor.application.ApplicationCall
import io.ktor.http.HttpMethod
import io.ktor.locations.*
import io.ktor.pipeline.PipelineContext
import io.ktor.routing.Route
import io.ktor.routing.method
import de.gfelbing.kotlin.swagger.models.*


// NOTE: ktor-location@0.9.0 is missing extension for Route.delete. This includes it.
inline fun <reified T : Any> Route.delete(noinline body: suspend PipelineContext<Unit, ApplicationCall>.(T) -> Unit): Route {
    return location(T::class) {
        method(HttpMethod.Delete) {
            handle(body)
        }
    }
}

object Paths {
    /**
     * 
     * 
     * @param userId  
     */
    @Location("/user/{userId}") class getUser(val userId: kotlin.Int)

}
Command line used for generation

I used the gradle plugin with the following config:

swaggerSources {
    demo {
        inputFile = file("src/main/resources/swagger.yaml")
        code {
            outputDir = file("$buildDir/swagger")
            language = "kotlin-server"
            library = "ktor"
            components = [
                    "supportingFiles": [
                            "Paths.kt"
                    ],
                    "models": true,
                    "apis": true
            ]
        }
        ui {
            outputDir = file("$buildDir/swagger/src/main/resources/swagger-ui")
        }
    }
}
Steps to reproduce

Generate the code using the config mentioned above.

Suggest a fix/enhancement

The template Paths.kt.mustache contains a check for bodyAllowed:

{{#operations}}
    {{#operation}}
    {{^bodyAllowed}}
    /**
     * {{summary}}
     * {{#unescapedNotes}}{{.}}{{/unescapedNotes}}
     {{#allParams}}* @param {{paramName}} {{description}} {{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
     {{/allParams}}*/
    @Location("{{path}}") class {{operationId}}({{#allParams}}val {{paramName}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}})

    {{/bodyAllowed}}
    {{/operation}}
{{/operations}}

Blindly removing it results in a NullPointerException.

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

Reproduce the kotlin-server generation with the Swagger YAML and Gradle configuration, then inspect Paths.kt.mustache and the generated Paths.kt. Trace the bodyAllowed check and the NullPointerException when it is removed. Done means post operations have locations in Paths.kt and generation completes successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
backend
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.