如何引用OpenAPI Generator任务属性

时间:2019-08-22 18:55:23

标签: swagger-codegen openapi-generator

在设置outputDir时,我试图引用分配的属性generatorName。

尝试使用与其他任务属性(即$ buildDir)相同的语法引用generatorName属性。还试图更完全限定属性名称openApiGenerator.generatorName。

openApiGenerate {
    verbose = false
    generatorName = "html2"   // assignment to property
    inputSpec = "$buildDir/swagger/testing.yml".toString()
    //outputDir = "$buildDir/generated".toString()
    outputDir = "$buildDir/generated/$generatorName".toString() // fails
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"

    // debugging code
    println "  buildDir:      $buildDir".toString()
    println "  generatorName: $generatorName".toString()  // this fails
}

调试代码的输出显示无法引用generatorName属性:

> Configure project :
  buildDir:      C:\Users\jgunchy\repos\testingproject\build
  generatorName: property(class java.lang.String, fixed(class java.lang.String, html2))

1 个答案:

答案 0 :(得分:1)

这是一个可观察的属性,而不是字符串。您应该可以使用.get()这样访问基础字符串:

openApiGenerate {
    verbose = false
    generatorName = "html2"
    inputSpec = "$buildDir/swagger/testing.yml".toString()
    outputDir = "$buildDir/generated/${generatorName.get()}".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
}

另一种选择是使用配置,而不是直接使用项目扩展容器的属性。例如,添加到gradle.properties

generatorName=html2

然后,您的配置将如下所示:

openApiGenerate {
    verbose = false
    generatorName = project.ext.generatorName
    inputSpec = "$buildDir/swagger/testing.yml".toString()
    outputDir = "$buildDir/${project.ext.generatorName}".toString()
    apiPackage = "org.openapi.example.api"
    invokerPackage = "org.openapi.example.invoker"
    modelPackage = "org.openapi.example.model"
}

$buildDir是Project实例上的一种吸气剂,它具有toString()方法,该方法恰巧输出文件路径,这就是它表现不同的原因。