swagger-codegen-generators icon indicating copy to clipboard operation
swagger-codegen-generators copied to clipboard

[java jaxrs-jersey] default values are never generated in annotations

Open tomzo opened this issue 7 years ago • 6 comments

In all operations I have in my 3.0 spec, swagger-codegen never emits @DefaultValue annotation, although it exists on parameter definitions.

For parameter in operation like this:

    put:
      operationId: putExactResultZip
      tags:
        - results
      parameters:
        - name: failed
          in: query
          schema:
            type: boolean
            default: false

Emitted code for parameter is

,@Parameter(description = "") @QueryParam("failed") Boolean failed

While I would expect it to include the default value:

,@Parameter(description = "") @QueryParam("failed") @DefaultValue("false") Boolean failed

It does not matter what is the type of default, in a big project with many uses of default I see no @DefaultValue annotations in generated code.

Version info

Swagger-codegen (build from 3.0.0 release 713d52883cf320d46fb1a555c63e8ad99f78c16e), which is using 1.0.0 release of swagger-codegen-generators.

tomzo avatar Sep 06 '18 17:09 tomzo

I've observed the same issue :( adding the following to my generator code shows no sign at all of the defaults in my spec in the output:

    @Override
    public CodegenOperation fromOperation(String path, String httpMethod, Operation operation, Map<String, Schema> schemas, OpenAPI openAPI) {

    	System.out.println("operation: " + operation.getOperationId());
    	System.out.println(operation);
    	System.out.println("schemas:");
    	System.out.println(schemas);
    	System.out.println("openAPI:");
    	System.out.println(openAPI);

        return super.fromOperation(path, httpMethod, operation, schemas, openAPI);
    }

Any hints on where I can change the code to fix this? Or is a fix on its way?

jbeaven avatar Oct 16 '18 13:10 jbeaven

@jbeaven I haven't tried yet, but it could possibly work in the community fork of the generator https://github.com/OpenAPITools/openapi-generator

tomzo avatar Oct 16 '18 17:10 tomzo

I too had this problem. When I looked into it, the defaultValue for a parameter was not propagated from the schema to the CodegenParameter class. For my project, I am using a custom code generator so I added this method to my Config class and now defaults are showing in generated code.

  /**
   * Need to copy the default value for a Schema query/header parameter to the CodegenProperty.
   */
  @Override
  public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) {
    CodegenParameter codegenParameter = super.fromParameter(parameter, imports);
    Object defaultValue = parameter.getSchema().getDefault();
    if (defaultValue != null) {
      codegenParameter.defaultValue = defaultValue.toString();
    }
    return codegenParameter;
  }

A long term fix would be to add the logic above in this method - https://github.com/swagger-api/swagger-codegen-generators/blob/master/src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java#L2256

douglasbgray avatar Nov 28 '18 22:11 douglasbgray

thanks for updating @douglasbgray . i'll try to get this solve next week.

HugoMario avatar Nov 28 '18 22:11 HugoMario

+1. The default value should be transferred to the CodegenParameter object.

mwiehl avatar May 03 '19 20:05 mwiehl

Hi @HugoMario, any chance to apply this PR? In our organization, we had to keep fork and apply changes manually.

ShemTovYosef avatar Dec 14 '25 09:12 ShemTovYosef