You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenAPI Generator生成Kotlin+Spring数据类的构造器语法错误

OpenAPI Generator 7.11.0升级后Kotlin数据类生成语法错误

环境版本信息

kotlinVersion = "2.1.10"
springBootVersion = "3.4.2"
springSecurityVersion = "6.4.2"
openAPIVersion= "7.11.0"
springVersion = "6.2.3"
openAPIKotlin = "1.8.0"

OpenAPI Generator配置

{
    "java17": "true",
    "dateLibrary": "java17",
    "serializationLibrary": "jackson",
    "enumPropertyNaming": "UPPERCASE",
    "reactive": "true",
    "modelMutable": "true",
    "useSpringBoot3": "true",
    "library": "spring-boot"
}

OpenAPI YAML中的Metadata定义

Metadata:
  description: Metadata fields contains the common RequestContext object and free form key-value pairs
  type: object
  properties:
    request_context:
      allOf:
        - $ref: "../../../../Context/v2/openapi.yaml#/components/schemas/Context"
      writeOnly: true
  additionalProperties: true

components:
  schemas:
    Context:
      description: Contextual information about the site/application invoking a service endpoint. VERY IMPORTANT!! This context is pretty much locked down! This does not mean it cannot change but changes to it affect a wide range of messages and service. Unless a change is really needed to expose a platform capability
      properties:
        locale:
          description: IETF BCP 47 language tag, e.g. "en-US".
          pattern: ^[a-z]{2}-[A-Z]{2}$
          type: string
        currency:
          description: ISO currency code, e.g. "USD".
          pattern: ^[A-Z]{3}$
          type: string
        device_context:
          $ref: '#/components/schemas/DeviceContext'
        experiments:
          description: Pre-evaluated experiments that can be passed down from clients to be consumed in various downstream services and extensions.
          type: array
          items:
            $ref: '#/components/schemas/Experiment'
        resource_owner:
          $ref: '#/components/schemas/UserIdentity'
        platform_product_id:
          description: To Partners(Including our brands), the EG Platform is a collection of public applications and services called "Platform Products" mastered in the control-plane which they can use to build their business. This attribute captures the Identifier for the Platform Product. For our brand sites, the id should reflect a travel-store.
          type: string
      type: object
    DeviceContext:
      description: Details of the device used by the user
      properties:
        device_user_agent_id:
          description: Anonymous device ID, e.g. DUAID (GUID)
          type: string
        cross_domain_id:
          description: Identifies a client across multiple points of sale for a browser
          type: string
        ip:
          description: The IP address of the user, typically from "X-Forwarded-For" HTTP header.
          type: string
        user_agent:
          description: The application type, operating system, software vendor or software version of the originating request.
          type: string
        device_type:
          $ref: '#/components/schemas/DeviceType'
        user_agent_context:
          $ref: '#/components/schemas/UserAgentContext'
      type: object
    DeviceType:
      default: DESKTOP
      description: Represents the various device types
      enum:
        - DEVICE_TYPE_INVALID
        - DESKTOP
        - MOBILE_PHONE
        - MOBILE_TABLET
        - APP_PHONE
        - APP_TABLET
      type: string
    Experiment:
      description: Represents an experiment evaluation, agnostic of a brand's experimentation framework.
      properties:
        id:
          description: Numeric ID, if one exists in the experimentation framework.
          format: int32
          type: integer
        name:
          description: Experiment name, if one exists in the experimentation framework.
          type: string
        bucket:
          description: The evaluated bucket, or treatment of the experiment.
          format: int32
          type: integer
      type: object
    UserIdentity:
      description: This is a reference to a human identity that can be used throughout the TravelAPI and not for use for authn purposes.
      properties:
        user_id:
          description: This should be an uri in the eg-user-id format i.e iam:auth:userid:eg:064eba0c-96f7-45ce-9a4e-506cf49e2ed3
          type: string
      type: object
    UserAgentContext:
      description: Traffic classification information about the user agent. If this field is not present, traffic should be assumed to be a human.
      properties:
        botness_score:
          description: Whether the user is a bot (+1) or a human (-1).
          type: number
        alignment_score:
          description: Whether the user is beneficial (+1) or malicious (-1).
          type: number
        classification:
          description: The functional category of the user if it was determined to be a bot.
          type: string

问题详情

升级OpenAPI Generator从5.1.1到7.11.0后,生成的Metadata数据类触发Kotlin语法错误,错误提示:

‘This type has a constructor, and thus must be initialized here. The braces are missing after kotlin.collections.HashMap<String, kotlin.Any>’

错误生成代码

data class Metadata(

    @field:Valid
    @Schema(example = "null", description = "")
    @get:JsonProperty("request_context") var requestContext: Context? = null
    ) : kotlin.collections.HashMap<String, kotlin.Any>{

    }

5.1.1版本正确生成代码

data class Metadata(

    @field:Valid
    @field:JsonProperty("request_context") var requestContext: Context? = null
) : kotlin.collections.HashMap<String, kotlin.Any>(){}

内容的提问来源于stack exchange,提问作者Amrita Chauhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 04:35:55