Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[Bug]: Cannot target java LocalDateTime via type mappings

未关闭
#12,650 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
java, openapi
领域
tooling

调研方向

通过 Maven 插件,使用提供的 OpenAPI 3.1 规范以及所示的 typeMappings 和 importMappings 重现该问题。首先跟踪 Java 生成器对已映射类型的保留字处理;当生成的代码使用 java.time.LocalDateTime 而不是 ModelLocalDateTime,并且能够编译时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Bug

Description

Using typeMappings I cannot specify a class with in-built support due to reserved word checking.

The OpenAPI specification defined "date-time" as a format value for strings, but that allows anything in ISO-8601. I want to declare in my spec whether it should be local date-time (no suffix), Zulu (Z suffix), or offer (+-nnnn suffix)

I am trying to do this with the following type mappings

typeMappings:
  string+date-time-local: LocalDateTime
  string+date: LocalDate
  string+time: LocalTime
  string+date-time-duration: Duration
  string+date-period: Period
  string+date-time-zoned: ZonedDateTime
  string+date-time-offset: OffsetDateTime
  string+timestamp: Instant

importMappings:
  LocalDateTime: 'java.time.LocalDateTime'
  LocalDate: 'java.time.LocalDate'
  LocalTime: 'java.time.LocalTime'
  ZonedDateTime: 'java.time.ZonedDateTime'
  OffsetDateTime: 'java.time.OffsetDateTime'
  Instant: 'java.time.Instant'
  Duration: 'java.time.Duration'
  Period: 'java.time.Period'

"LocalDate" is treated as a reserved word to avoid conflict between a spec declaring a model of that name and the java.time classes. I do not want to declare anything new but instead actually use the standard java.time classes.

When doing this, I receive this warning [WARNING] LocalDate (reserved word) cannot be used as model name. Renamed to ModelLocalDate, and it generates code using ModelLocalDate. I cannot see any obvious way of achieving what I need to do,.

Swagger Codegen Version

7.13.0 (via maven plugin)

Language / Generator

Java

OpenAPI/Swagger Spec

openapi: 3.1.0
info:
  title: Example
  version: 1.0.0
paths:
  /example:
    post:
      summary: example
      operationId: example
      parameters:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Example"
components:
  schemas:
    Example:
      type: object
      properties:
        localDate:
          type: string
          format: date-time-local

Steps to Reproduce

Attempt to generate the above OpenAPI specification using the type mappings above.

Expected Behaviour

Code is generated which utilised java.time.LocalDateTime

Actual Behavior

Code is generated using ModelLocalDateTime, which does not compile

Related

This would appear to be related to the changes in https://github.com/OpenAPITools/openapi-generator/pull/16276

Checklist

  • I have searched the existing issues to make sure this is not a duplicate.
  • I have included a minimal and reproducible spec example.
  • I have explained how to reproduce the issue.
  • I have specified which generator/language is affected.
主要语言
Mustache
星标
17.8k
派生
6k
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

swagger-api/swagger-codegen 的其他 Issue

查看 swagger-api/swagger-codegen 的全部 Issue

相似的 Issue

更多 DevTools Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。