You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
$ 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.
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.
@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
Bug Report Checklist
Description
JSON schema allows additional properties by default, see https://json-schema.org/understanding-json-schema/reference/object#additionalproperties
But Go generator produces
UnmarshalJSON()
withdecoder.DisallowUnknownFields()
regardless of theadditionalProperties
flag.openapi-generator version
OpenAPI declaration file content or url
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 containdecoder.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()
The text was updated successfully, but these errors were encountered: