如何使用 swagger codegen 生成带有自定义日期时间注释的模型

How to produce a model with a custom Datetime annotation using swagger codegen

我们需要使用以下注释来注释日期时间字段:

@Type("DateTime<'Y-m-d\TH:i:sP'>")

有人可以建议如何使用 swagger code gen 来实现这一点。代码库是 PHP。 当前字段定义如下:

created:
        type: "string"
        format: "date-time"
        description: "Date client details first appeared in the system."         
        default: null

所需输出:

/**
     * Date client details first appeared in the system.
     *
     * @var \DateTime|null
     * @SerializedName("createdDate")
     * @Assert\DateTime()
     * @Type("DateTime<'Y-m-d\TH:i:sP'>")
     */
    protected $createdDate;

swagger codegen 正在生成什么:

 /**
     * Date client details first appeared in the system.
     *
     * @var \DateTime|null
     * @SerializedName("createdDate")
     * @Assert\DateTime()
     * @Type("DateTime")
     */
    protected $createdDate;

Swagger Codegen 使用 Mustache 模板生成代码。例如,示例中的 PHP 注释在此模板中定义:

https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/resources/php-symfony/model_variables.mustache

您可以修改这些模板以自定义输出。


将上面的模板下载到您的计算机并根据需要更改日期时间注释。然后 运行 codegen 使用 -t 参数来指定自定义模板的路径:

java -jar swagger-codegen-cli-2.4.4.jar generate
  -i http://petstore.swagger.io/v2/swagger.json
  -l php-symfony
  -o petstore_php_server
  -t path/to/MyTemplates    <------

将使用在 -t 文件夹中找到的任何自定义模板,而不是相应的标准模板。在 -t 文件夹中找不到的模板将默认为标准模板。