Search code examples
springapigradleswaggerswagger-codegen

How to connect importMappings and yaml in Swagger


I have Spring RESTfull application and I want to generate an API for it. I use a gradle configuration from there https://github.com/Casturan/swagger-gradle-example/blob/master/build.gradle to generate code. But there is problem it uses models defined in definitions: while I want it to use my models from shared module. I found that I need to use importMapping but when I try to apply this command in my build.gradle I am getting an error:

importMappings = [
   'board_container': 'board_container=com.workingbit.share.domain.impl.BoardContainer'
]
> Could not set unknown property 'importMappings' for task ':myproject:generateApi' of type org.gradle.api.DefaultTask.

So question how to use importMapping and how to connect it with my model in yaml?


Solution

  • I haven't tried it but looking at the code of CodegenConfigurator modifying the generateApi task like this in the build.gradle might work:

    task generateApi {
      inputs.file("$projectDir/$swaggerSourceFile")
      outputs.dir("$projectDir/$swaggerTargetFolder")
      doLast{
        def config = new CodegenConfigurator()
        config.setInputSpec("file:///$projectDir/$swaggerSourceFile")
        config.setOutputDir("$projectDir")
        config.setLang('spring')
        config.setAdditionalProperties([
            'interfaceOnly' : 'true',
            'apiPackage'    : 'com.dturan.api',
            'modelPackage'  : 'com.dturan.model',
            'sourceFolder'  : swaggerTargetFolder
        ])
        //Add this line
        config.addImportMapping("board_container", "com.workingbit.share.domain.impl.BoardContainer")
        new DefaultGenerator().opts(config.toClientOptInput()).generate()
      }
    }
    

    The CodegenConfigurator is being called at the second line in the build.gradle you linked to in your question and it has a few methods to configure importMappings. You can have a look here (if this doesn't work try with the setImportMappings).