OpenAPITools / OpenAPITools/openapi-generator

[BUG] DefaultCodegen#fromProperty caches dirty properties and causes compilation error in Kotlin

Open
#17,446 2 comments 0 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
openapi-generator version

7.1.0

OpenAPI declaration file content or url
openapi: '3.0.1'
info:
  version: '1'
  title: 'test'
  description: 'test'

security:
  - adfs_user: []
paths: {}
components:
  schemas:

    SealedFoo:
      type: object
      properties:
        fuzz:
          type: string
        discriminatorValue123:
          type: string
          description: SealedFoo discriminator. Do not remove this description!
      discriminator:
        propertyName: discriminatorValue123
        mapping:
          bar: '#/components/schemas/Bar'
          baz: '#/components/schemas/Baz'
      required:
        - fuzz
        - discriminatorValue123

    Bar:
      allOf:
        - $ref: '#/components/schemas/SealedFoo'
        - type: object
          properties:
            bar:
              type: string

    Baz:
      allOf:
        - $ref: '#/components/schemas/SealedFoo'
        - type: object
          properties:
            baz:
              type: string

    Whatever:
      title: Whatever
      type: object
      required:
        - discriminatorValue123
        - text
      properties:
        discriminatorValue123:
          type: string
        text:
          type: string
Generation Details

DefaultCodegen, kotlin server

Generated model for Whatever will have isOverride flag set to true, which cause compilation error.

Steps to reproduce

Whatever.discriminatorValue123 will be marked with override if Whatever is placed after Bar and Baz.

Also, any modification of SealedFoo.discriminatorValue123, say adding a description, will fix this issue.

Suggest a fix

This happens because of usage of cache in org.openapitools.codegen.DefaultCodegen#fromProperty(java.lang.String, io.swagger.v3.oas.models.media.Schema, boolean, boolean):

        CodegenProperty cpc = schemaCodegenPropertyCache.get(ns);

After processing Bar or Baz a CodegenProperty with isOverride = true placed into cache, and while processing Whatever this property is retrieved from cache as-is.

I suggest remove the cache till you completely sure it doesn't break anything in a such stupid way.

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

Start at org.openapitools.codegen.DefaultCodegen#fromProperty(java.lang.String, io.swagger.v3.oas.models.media.Schema, boolean, boolean) and inspect schemaCodegenPropertyCache. Reproduce with the supplied OpenAPI declaration using the Kotlin server generator, placing Whatever after Bar and Baz; done means Whatever.discriminatorValue123 no longer receives the incorrect isOverride flag and the generated Kotlin compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.