Skip to content

how to put parameter default value into generated code in swagger #6782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wangyiran125 opened this issue Oct 22, 2017 · 14 comments
Closed

how to put parameter default value into generated code in swagger #6782

wangyiran125 opened this issue Oct 22, 2017 · 14 comments

Comments

@wangyiran125
Copy link

swagger parameter default is not in generated code. for example:

paths:
/user/refresh.json:
post:
operationId: refresh
description: |
By passing in the appropriate options, you can search for
available inventory in the system
produces:
- application/json
consumes:
- application/x-www-form-urlencoded
parameters:
- name: "RC-App-Key"
in: "header"
description: appkey..
required: true
default: "abc"
type: "string"

the generated code like

public class DevelopersApiTest {

private final DevelopersApi api = new DevelopersApi();

@test
public void refreshTest() throws ApiException {
String rcAppKey = null;
Common response = api.refresh(rcAppKey);

// TODO: test validations

}
the rcAppKey is null,but i want it be the default value "abc",like:

String rcAppKey = "abc";
Common response = api.refresh(rcAppKey);
the default value is not in generated code,but in .md file,how to put the default value into the code?

@wangyiran125
Copy link
Author

i use swagger2.0

@wing328
Copy link
Contributor

wing328 commented Oct 22, 2017

Please try the latest master (2.3.0), which has better support for default values.

SNAPSHOT version can be found in the README

@wangyiran125
Copy link
Author

@wing328 hi, i use 2.3.0 to generate java code, the default value is generated in DevelopersApi.md file, but not in java code.

@jonschoning
Copy link
Contributor

I don't know about how the Java generator is supposed to work, but according to https://swagger.io/unlocking-the-spec-the-default-keyword/ the client does not need to set the default; it is the server that should assume the default value when it is not received by the client. So it shouldn't be necessary to set the default on the client when communicating to a server that is following the spec appropriately.

@wangyiran125
Copy link
Author

@jonschoning thanks,i use swagger3.0,and the swagger-codegen-cli-3.0.0-20171009.075709-6.jar,my command is java -jar swagger-codegen-cli-3.0.0-20171009.075709-6.jar generate -i ~/Downloads/swagger.json --library=okhttp-gson -l java -o /var/tmp/java_api_client,but exception [main] WARN io.swagger.codegen.ignore.CodegenIgnoreProcessor - Output directory does not exist, or is inaccessible. No file (.swager-codegen-ignore) will be evaluated.
Exception in thread "main" java.lang.RuntimeException: missing swagger input or config!
at io.swagger.codegen.DefaultGenerator.generate(DefaultGenerator.java:687)
at io.swagger.codegen.cmd.Generate.run(Generate.java:285)
at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)

@wing328
Copy link
Contributor

wing328 commented Oct 23, 2017

@jonschoning is right in saying that the API client does not take the default value ({{{defaultValue}}}) into account as the server should be setting the default value. (now I recall a similar discussion 2 years ago!)

@wing328
Copy link
Contributor

wing328 commented Oct 23, 2017

Exception in thread "main" java.lang.RuntimeException: missing swagger input or config!

You may want to double check the spec.

@wangyiran125
Copy link
Author

@wing328 thanks,missing swagger input or config! may be a bug
like:#6517 (comment)

@wing328
Copy link
Contributor

wing328 commented Oct 23, 2017

@wangyiran125 can you try the 2.3.0 SNAPSHOT instead?

@wangyiran125
Copy link
Author

@wing328 tried 2.3.0,but the same exception when i use swagger 3.0 Exception in thread "main" java.lang.RuntimeException: missing swagger input or config!

@wing328
Copy link
Contributor

wing328 commented Oct 24, 2017

@wangyiran125 can you please share the spec (e.g. via gist.github.com)?

@dfrommi
Copy link
Contributor

dfrommi commented May 12, 2020

Is there an update on the issue?
I'm using swagger-codegen 3.0.8 and have the same issue. The default value is not showing up in the model.

It's not an template-issue. I've used a debugger to verify the CodegenModel and while minimum and maximum are properly set, default is null in the CodegenParameter.

Here's an enhanced operation of the pet store having the issue:

  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - petstore
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            minimum: 1
            maximum: 100
            default: 20
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:    
              schema:
                $ref: "#/components/schemas/Pets"

@HugoMario
Copy link
Contributor

hi @dfrommi
can you please try with recent with latest version ?

@dfrommi
Copy link
Contributor

dfrommi commented May 13, 2020

Hi @HugoMario

Took a bit now to test it, because I'm using a custom generator and had to fix some things for the update.

Yes, it is working with version 3.0.19

Thanks a lot and sorry for the confusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants