Skip to content

Default to OAS-compliant handling of additionalProperties #21170

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ctreatma
Copy link
Contributor

@ctreatma ctreatma commented Apr 28, 2025

This changes the default value for disallowAdditionalPropertiesIfNotPresent from true to false, which means that the generator will now, by default, produce code that complies OpenAPI standards with respect to the handling of additionalProperties.

Note that a number of example configs did not have to be updated as a result of this change, which indicates that the disallowAdditionalPropertiesIfNotPresent option had no effect on the generators used in those examples.

Closes #21169

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 || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    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*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.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 force-pushed the default-oas-compliant branch from 7c220fc to b38c2fb Compare May 1, 2025 20:43
@ctreatma
Copy link
Contributor Author

ctreatma commented May 1, 2025

@wing328 I took a crack at swapping the default value of disallowAdditionalPropertiesIfNotPresent so that, in a future major version, openapi-generator can default to generating OAS-compliant code instead of defaulting to the old non-compliant behavior. I'm running into some unexpected changes in the sample code and could use your help understanding what's happening there.

@@ -5,3 +5,4 @@ templateDir: modules/openapi-generator/src/main/resources/python-fastapi
sourceFolder: "src"
additionalProperties:
hideGenerationTimestamp: "true"
disallowAdditionalPropertiesIfNotPresent: true
Copy link
Contributor Author

@ctreatma ctreatma May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 In the bin/configs/python-fastapi.yaml, as in many other sample configs, I added disallowAdditionalPropertiesIfNotPresent: true to avoid a PR that has massive changes across all examples. I expected that the samples would have 0 changes as a result, because true was the default value prior to this PR.

Comment on lines +101 to +105
# raise errors for additional fields in the input
for _key in obj.keys():
if _key not in cls.__properties:
raise ValueError("Error due to additional fields (not defined in Order) in the input: " + _key)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the sample code corresponding to the config I mentioned in #21170 (comment).

This code is being updated to throw errors when there are additional properties, but since the old default value was disallowAdditionalPropertiesIfNotPresent=true, these models should have already been throwing errors before I made my change. I'm not sure what I'm missing here. Is there some hidden third case for some languages when disallowAdditionalPropertiesIfNotPresent isn't explicitly set to a true or false?

@@ -152,7 +152,7 @@ Authentication schemes defined for the API:
- apiName: Api
- caseInsensitiveResponseHeaders:
- conditionalSerialization: false
- disallowAdditionalPropertiesIfNotPresent:
- disallowAdditionalPropertiesIfNotPresent: true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change in the README for one of the C# samples seems to reinforce the idea that the default value for disallowAdditionalPropertiesIfNotPresent is a hidden third option rather than true.

@ctreatma ctreatma force-pushed the default-oas-compliant branch from 69520f0 to 9ef6192 Compare May 2, 2025 15:55
@ctreatma ctreatma force-pushed the default-oas-compliant branch from 9ef6192 to 2d4fb51 Compare May 2, 2025 16:14
Map<String, String> disallowAdditionalPropertiesIfNotPresentOpts = new HashMap<>();
disallowAdditionalPropertiesIfNotPresentOpts.put("false",
"The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.");
disallowAdditionalPropertiesIfNotPresentOpts.put("true",
"Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.");
disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts);
cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt);
this.setDisallowAdditionalPropertiesIfNotPresent(true);
this.setDisallowAdditionalPropertiesIfNotPresent(false);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious: why also set the default value here? Does the default value not flow through automatically via the CliOption?

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

Successfully merging this pull request may close these issues.

[REQ] Change disallowAdditionalPropertiesIfNotPresent default to false
1 participant