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

[Go] validate required fields when unmarshalling JSON #16863

Merged
merged 3 commits into from
Nov 4, 2023

Conversation

ctreatma
Copy link
Contributor

@ctreatma ctreatma commented Oct 18, 2023

Relates to #12201

Code generated by the Go client generator does not validate required fields, which is especially problematic in oneOf schemas where the sub-schemas are differentiated by required fields.

For example, consider the following schema:

components:
  schemas:
    Thing:
      oneOf:
        - $ref: '#/components/schemas/ThingWithFoo'
        - $ref: '#/components/schemas/ThingWithBar'
    ThingWithFoo:
      type: object
      properties:
        foo:
          type: string
        id:
          type: string
    required:
      - foo
    ThingWithBar:
      type: object
      properties:
        bar:
          type: string
        id:
          type: string
    required:
      - bar

Assume that the API itself only returns an object that has foo or an object that has bar; it will not return an object with both properties. Existing versions of openapi-generator Go client generator will generate models that do not enforce required properties; as a result, any attempt to use the Thing model will result in an error like Error when calling <something>: data matches more than one schema in oneOf(Thing).

This PR replicates the required field validation that exists in the okhttp-gson generator:

  • Models with required properties are generated with an array of required property names
  • UnmarshalJSON is updated to unmarshal the JSON into a map with string keys
  • If a required property is not found in the map, UnmarshalJSON returns an error

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh ./bin/configs/*.yaml
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (upcoming 7.1.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@ctreatma ctreatma changed the title validate required properties when unmarshalling JSON [Go] validate required fields when unmarshalling JSON Oct 18, 2023
@ctreatma ctreatma marked this pull request as ready for review October 18, 2023 19:30
@ctreatma ctreatma force-pushed the go-client-required branch 2 times, most recently from 268ecb5 to a5fc00e Compare October 18, 2023 21:49
@wing328
Copy link
Member

wing328 commented Oct 19, 2023

thanks for the PR.

cc @antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @jirikuncar (2021/01) @ph4r5h4d (2021/04) @lwj5 (2023/04)

@wing328
Copy link
Member

wing328 commented Oct 19, 2023

can you please take a look at the CI failure when you've time?

https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/26176/workflows/45077b93-6bb7-4281-8c86-64b3d134cde5/jobs/80635

@ctreatma ctreatma force-pushed the go-client-required branch 3 times, most recently from f1c8b9e to 01479c4 Compare October 23, 2023 16:51
@ctreatma
Copy link
Contributor Author

@wing328 I updated the PR to only generate the UnmarshalJSON method when a model has additional properties or required properties & addressed the CI failures. When you have a moment, please take a look and let me know if there's anything you'd like me to change.

@wing328
Copy link
Member

wing328 commented Oct 29, 2023

overall it looks good. when you've time, can you please PM me via Slack for a few quick questions/feedback?

@wing328 wing328 added this to the 7.1.0 milestone Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants