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] UnmarshalJSON() does not allow unknown fields even when they are not forbidden in schema #18613

Closed
5 of 6 tasks
dyurovskykh-tivo opened this issue May 8, 2024 · 3 comments

Comments

@dyurovskykh-tivo
Copy link

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

JSON schema allows additional properties by default, see https://json-schema.org/understanding-json-schema/reference/object#additionalproperties

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

The value of the additionalProperties keyword is a schema that will be used to validate any properties in the instance that are not matched by properties or patternProperties. Setting the additionalProperties schema to false means no additional properties will be allowed.

But Go generator produces UnmarshalJSON() with decoder.DisallowUnknownFields() regardless of the additionalProperties flag.

openapi-generator version
  • latest
OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Test
  version: 1.0.0
paths: {}
components:
  schemas:
    Object:
      required:
        - field1
      properties:
        field1:
          description: Something
          type: string
Generation Details

$ docker run --rm -u 1001:1001 -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/test.yaml -g go -o /local/out/go

Steps to reproduce

Run generator and check the generated code. UnmarshalJSON() must not contain decoder.DisallowUnknownFields() because additional properties are not forbidden.

Related issues/PRs

Introduced by #17575

Suggest a fix

Place the following LOC under the condition to be added only when additionalProperties = false for a given structure:
decoder.DisallowUnknownFields()

@dyurovskykh-tivo
Copy link
Author

@wing328 @ctreatma
Please, check this bug.

@ctreatma
Copy link
Contributor

ctreatma commented May 8, 2024

The model template only generates the decoder.DisallowUnknownFields() code if the generator thinks that additional properties are not allowed: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/go/model_simple.mustache#L493-L511

What config are you passing in when running the generator? There is a disallowAdditionalPropertiesIfNotPresent config option that controls the behavior of generated code for all languages: https://openapi-generator.tech/docs/generators/go/#config-options

For legacy reasons, the disallowAdditionalPropertiesIfNotPresent defaults to true. This means that by default, the generator will produce models that assume additional properties are not allowed if the additionalProperties key is not explicitly specified.

To configure the generator to produce code that complies with the additionalProperties behavior documented in OAS, you have to set disallowAdditionalPropertiesIfNotPresent to false.

@dyurovskykh-tivo
Copy link
Author

@ctreatma
Thank you! I did not know about disallowAdditionalPropertiesIfNotPresent config.
When I run the generator like docker run --rm -u 1001:1001 -v "${PWD}:/local" openapitools/openapi-generator-cli generate --additional-properties=disallowAdditionalPropertiesIfNotPresent=false -i /local/test.yaml -g go -o /local/out/go, decoder.DisallowUnknownFields() is not generated

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

2 participants