swagger-api / swagger-api/swagger-codegen

[Java] set artifactVersion template variable to mirror Swaggerfile info.version by default

Open
#8,127 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When using the swagger-codegen-cli tool to generate Java client source files, I have to explicitly set the artifactVersion variable either as a CLI option or a json config to keep the output client in sync with the release version specified in the Swaggerfile. It would be helpful to read the artifactVersion from the Swagger file by default so that we can manage versioning from a single source of truth, e.g. how the JavaScript generator reads the projectVersion from the Swagger file.

Swagger-codegen version

I have been using the latest stable release of the CLI, currently v2.3.1.

Swagger declaration file content or url

service-swagger-schema.yaml

swagger: "2.0"
info:
  description: "Swagger definition for xxxxx"
  version: "1.1.0"
  ...

swagger-codegen-resources/java/java-config.json

{
  "groupId": "xxx.xxx.xxxxx",
  "artifactId": "xxx-xxx-xxxxx"
}

swagger-codegen-resources/java/pom.mustache

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>{{groupId}}</groupId>
    <artifactId>{{artifactId}}</artifactId>
    <packaging>jar</packaging>
    <name>{{artifactId}}</name>
    <version>{{artifactVersion}}</version>
    ...
Command line used for generation

$ mkdir java-client
$ java -jar swagger-codegen-cli-2.3.1.jar generate -i ./service-swagger-schema.yaml -l java -o java-client -t swagger-codegen-resources/java -c swagger-codegen-resources/java/java-config.json

Steps to reproduce

Running the above commands will generate the Java client source files. Looking at the pom.xml file in the output java-client directory, the version is just the default 1.0.0 version specified in the AbstractJavaCodegen class instead of version 1.1.0 specified in the Swagger file.

Suggest a fix/enhancement

I'm not sure if falling back to v1.0.0 by default is by design, but if there is no explicit decision to make that the default it would be nice to modify the AbstractJavaCodegen.preprocessSwaggermethod to set the artifactVersion to info.versionby default instead.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in AbstractJavaCodegen.preprocessSwagger and compare how the JavaScript generator reads projectVersion from the Swagger file. Use service-swagger-schema.yaml with the Java configuration and inspect the generated pom.xml; done means info.version supplies artifactVersion by default instead of 1.0.0 when no explicit value is provided.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.