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

[BUG][Go] Default string values of enums are not double quoted #14665

Closed
5 of 6 tasks
jlsherrill opened this issue Feb 10, 2023 · 2 comments · Fixed by #14684
Closed
5 of 6 tasks

[BUG][Go] Default string values of enums are not double quoted #14665

jlsherrill opened this issue Feb 10, 2023 · 2 comments · Fixed by #14684

Comments

@jlsherrill
Copy link
Contributor

jlsherrill commented Feb 10, 2023

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When trying to generate a Go client binding for pulp (https://docs.pulpproject.org/pulp_rpm/_static/api.json), default values that are strings do not seem to be double quoted:

For example, a generated model for the mentioned spec is:

func NewRpmRpmRemote(name string, url string) *RpmRpmRemote {
        this := RpmRpmRemote{}
        this.Name = name
        this.Url = url
        var policy PolicyEnum = immediate
        this.Policy = &policy
        return &this
}

This fails to compile, and the generated tests fail with:

./model_rpm_rpm_remote.go:86:26: undefined: immediate

I would expect immediate to be wrapped in double quotes:

        var policy PolicyEnum = "immediate"

I suspect that this is related to toDefaultValue() here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java#L391-L405

Its either not being called, or is not detecting the string properly. The value mentioned is an enum and from testing with a simplified example, that is the root of the problem

openapi-generator version

6.4.0-SNAPSHOT
2023-02-09T02:46:35Z

OpenAPI declaration file content or url

http://localhost:8080/pulp/api/v3/docs/api.json

i've created a simpler example here: https://gist.github.com/jlsherrill/af376f50ca7692332ab19ce01d88ee11

Generation Details
          docker run --network=host --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
           -i http://localhost:8080/pulp/api/v3/docs/api.json \
           -g go \
          -o /local/packages/pulpGoBinding  --skip-validate-spec \
           --git-user-id=user_id --git-repo-id=repo_id \
           --package-name=pulpGoBinding \
           -p enumClassPrefix=true \
           -p structPrefix=true \
           -p disallowAdditionalPropertiesIfNotPresent=true
Steps to reproduce

Generate go bindings with the above command, and then run the resulting tests:

$  go test ./...
./model_patchedrpm_rpm_remote.go:74:26: undefined: immediate
./model_patchedrpm_rpm_remote.go:84:26: undefined: immediate
./model_patchedrpm_uln_remote.go:74:26: undefined: immediate
./model_patchedrpm_uln_remote.go:84:26: undefined: immediate
./model_rpm_rpm_remote.go:76:26: undefined: immediate
./model_rpm_rpm_remote.go:86:26: undefined: immediate
./model_rpm_rpm_remote_response.go:74:26: undefined: immediate
./model_rpm_rpm_remote_response.go:84:26: undefined: immediate
./model_rpm_uln_remote.go:78:26: undefined: immediate
./model_rpm_uln_remote.go:88:26: undefined: immediate
./model_rpm_uln_remote.go:88:26: too many errors

Related issues/PRs
Suggest a fix

Currently working to get a build going to try to debug further.

@jlsherrill jlsherrill changed the title [BUG][Go] Default string values are not double quoted [BUG][Go] Default string values of enums are not double quoted Feb 10, 2023
@jlsherrill
Copy link
Contributor Author

jlsherrill commented Feb 10, 2023

after digging into this more, I'm not sure that this is a bug, or at least its an easy to workaround one. In the spec that was given, we see a property with allOf{}, but only one type set:

                    "policy": {
                        "allOf": [
                            {
                                "$ref": "#/components/schemas/PolicyDb6Enum"
                            }
                        ],
                        "default": "immediate",
                        "description": "The policy to use when downloading content."
                    },

So, for it to know how to write the default, it would have to consider all possibilities within allOf. Here there is only one possibility, so rewriting it like so:

                    "policy": {
                       "$ref": "#/components/schemas/PolicyDb6Enum"
                        "default": "immediate",
                        "description": "The policy to use when downloading content."
                    },

makes much more sense and results in a valid client. I will file a pulp bug

@jlsherrill
Copy link
Contributor Author

Reopening, as a pulp developer mentioned that $ref in a property overrides siblings, (such as default and description), so this bug still seems valid and not easily worked around

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

Successfully merging a pull request may close this issue.

1 participant