Skip to content
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

[Java] Support enums in query params #1347

Open
cbornet opened this issue Oct 7, 2015 · 13 comments
Open

[Java] Support enums in query params #1347

cbornet opened this issue Oct 7, 2015 · 13 comments

Comments

@cbornet
Copy link
Contributor

cbornet commented Oct 7, 2015

In Java generation, enums are generated when they are part of a definition object but not when they are a direct method property.
For instance:

"/pet/findByStatus": {
      "get": {
        "tags": [
          "pet"
        ],
        "summary": "Finds Pets by status",
        "description": "Finds Pets by status",
        "operationId": "findPetsByStatus",
        "produces": [
          "application/json",
          "application/xml"
        ],
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "description": "Status value that for filter",
             "type": "string",
             "enum": ["available", "pending", "sold"]
            }
         ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/Pet"
              }
            }
          }
        }

Generates "status" as a String not as an enum.

@fehguy fehguy added this to the v2.1.5 milestone Oct 26, 2015
@fehguy
Copy link
Contributor

fehguy commented Oct 26, 2015

This can be tricky. When parameters are passed and mapped directly to enums, any name mismatching will cause big issues on the server side, typically things that you would need to handle on your own unless you're using some Jackson features for deserializing into enum properties

It is valid to expect, and probably worth keeping open and implementing, but it can have some negative side effects.

@fehguy fehguy modified the milestones: v2.1.5, v2.1.6 Jan 6, 2016
@vivin
Copy link
Contributor

vivin commented Feb 12, 2016

@fehguy This is an issue I am running into as well. It is tricky, to be sure! I'm using the Feign library and have a template that generates a client that works with Feign. In Feign you can specify custom "expanders" (basically serializers) to convert an arbitrary object into an representation that is query-string safe.

Currently it appears that the model built up by swagger-codegen ignores the enum information when building up a CodegenParameter instance. This is especially the case when the query parameter is an array of enums. It appears that all the data exposed by swagger is correctly recognized, but it is not attached to the CodegenParameter instance. For example, I have the following schema that describes a parameter:

{
    "collectionFormat": "multi",
    "enum": [
        "UNKNOWN",
        "CUSTOMER",
        "LEAD",
        "PARTNER",
        "COWORKER",
        "PERSONAL",
        "SYSTEM",
        "VISITOR",
        "ANONYMOUS",
        "VENDOR"
    ],
    "in": "query",
    "items": {
        "enum": [
            "UNKNOWN",
            "CUSTOMER",
            "LEAD",
            "PARTNER",
            "COWORKER",
            "PERSONAL",
            "SYSTEM",
            "VISITOR",
            "ANONYMOUS",
            "VENDOR"
        ],
        "type": "string"
    },
    "name": "classifications",
    "required": false,
    "type": "array"
}

Currently I just end up with the type as List<String> (which isn't necessarily incorrect), but I would like to have the model include the type information for enums so that I can generate my client as I see fit.

I'm not too familiar with the code, but I don't mind taking a stab at this - it might amount to simply attaching the data that swagger-codegen already finds, to the CodegenParameter object. This way we at least have access to the information.

Let me know what you think; thanks!

@vivin
Copy link
Contributor

vivin commented Feb 16, 2016

@wing328 Any thoughts on this? I could submit a PR for this too. I'll see if I can take a first pass at this sometime this week.

vivin added a commit to vivin/swagger-codegen that referenced this issue Feb 17, 2016
This is a fix to support enums in query parameters. Enum-related information was not being stored on `CodegenParameter` previously; it is now. Test cases have been added to make sure that the enum information is being properly processed from the model.
vivin added a commit to vivin/swagger-codegen that referenced this issue Feb 26, 2016
This is a fix to support enums in query parameters. Enum-related information was not being stored on `CodegenParameter` previously; it is now. Test cases have been added to make sure that the enum information is being properly processed from the model.
wing328 added a commit that referenced this issue Feb 27, 2016
Support for enums in query parameters (issue #1347)
@wing328
Copy link
Contributor

wing328 commented Mar 22, 2016

@vivin sorry that I missed your message. Yes, please submit a PR if you've time and we'll review accordingly.

@devaxt
Copy link

devaxt commented Dec 8, 2016

Is it planned ? or canceled ?

@wing328
Copy link
Contributor

wing328 commented Dec 9, 2016

@devaxt I would suggest you to pull the latest master to give it a try if you've not done so as there's been a lot of enhancements to swagger codegen since March. If it's still an issue, please let us know with the details such as the code generated and what you actually expected.

@MatanRubin
Copy link
Contributor

What's the current status of this one? I see that @vivin pushed a fix for this in c3c73b8, and yet I see this issue still exists on latest master (using JAXRS-CXF and Java generators). What am I missing?

@wing328 wing328 modified the milestones: v2.2.3, v2.2.2 Feb 15, 2017
@wing328
Copy link
Contributor

wing328 commented Feb 15, 2017

@MatanRubin seems like it's still not yet supported. If you want to contribute the enhancement, please file a PR.

@vivin
Copy link
Contributor

vivin commented Feb 23, 2017

@wing328 Did that commit I made make it in? It looks like the templates will need to be changed to support this, correct?

@cbornet
Copy link
Contributor Author

cbornet commented Feb 24, 2017

@vivin yes, the templates need to be changed. There's a PR for Spring.

@michelecappelletti
Copy link

@vivin yes, the templates need to be changed. There's a PR for Spring.

Which PR?? I'm interested in, can u link us the PR pls?

@cbornet
Copy link
Contributor Author

cbornet commented Mar 25, 2017

see #3198

@wing328 wing328 modified the milestones: Future, v2.2.3 Jul 13, 2017
@jacobweber
Copy link

I think it's important to have some kind of solution to this for all languages, not just Java. Even if it means just putting the enumerated values into the documentation, there needs to be some way for the user to know the possible values of a parameter.

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

No branches or pull requests

8 participants