OpenAPITools / OpenAPITools/openapi-generator

[BUG] [kotlin-spring] Type mismatch for post -> required)true

Open
#10,832 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

(The generation works perfectly fine for typescript-angular)

The generated post request expects a "SearchRequest" object, the generated test provides a "SearchRequest?", thus failing

SearchApiTest.kt:[24,75] Type mismatch: inferred type is SearchRequest? but SearchRequest was expected

@Test
    fun searchPostTest() {
        val searchRequest:SearchRequest? = null
        val response: ResponseEntity<List<SearchResult>> = api.searchPost(searchRequest)

        // TODO: test validations
    }



    fun searchPost( @Valid @RequestBody searchRequest: SearchRequest
): ResponseEntity<List<SearchResult>> {
        return ResponseEntity(HttpStatus.NOT_IMPLEMENTED)
    }

Since we use the generated code as a library to be imported in other projects, it's an inconvenience

openapi-generator version

openapi: 3.0.1
penapitools/openapi-generator-cli:v5.3.0

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Search
  version: v1
servers:
  - url: /api/v1
paths:
  /search:
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SearchRequest'
      responses:
        "200":
          description: Ad Spaces found
          content:
            application/json:
              schema:
                items:
                  $ref: '#/components/schemas/SearchResult'
                type: array
components:
  schemas:
    SearchRequest:
      type: object
      properties:
        types:
          type: string
    SearchResult:
      type: object
      properties:
        name:
          type: string
Generation Details

image: openapitools/openapi-generator-cli:v5.3.0
docker-entrypoint.sh generate -i ./openapi/openapi.yaml -g kotlin-spring -o ./build --additional-properties=artifactVersion=${VERSION} --additional-properties=artifactId=api-server

Steps to reproduce
Related issues/PRs
Suggest a fix

In case of required, don't use "null" but instead create an empty object?

I hope there is something i'm missing or a quickfix besides skipping tests!

Thanks and best regards

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

Use the supplied OpenAPI YAML with the documented kotlin-spring generation command to reproduce the failure. Compare the generated SearchApiTest.kt call with the generated searchPost signature and determine how required request bodies should be represented. Done means the generated Kotlin tests compile without the nullable-versus-non-nullable type mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, spring
Domain
api, backend, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.