OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][jaxrs-cxf] Option disallowAdditionalPropertiesIfNotPresent has no effect

Open
#10,645 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 setting disallowAdditionalPropertiesIfNotPresent to false, the generator should add support for additional properties to model classes for schemas that have no additionalProperties attribute. This is explicitly stated in the documentation of the generator:

If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.

Instead, the generated code is the same, independent of the value for disallowAdditionalPropertiesIfNotPresent.

openapi-generator version

5.2.1

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  version: "v1"
  title: Example
paths: {}
components:
  schemas:
    Example:
      type: object
      properties:
        a:
          type: integer
Generation Details
openapi-generator-cli generate \
-i openapi.yaml \
--additional-properties=disallowAdditionalPropertiesIfNotPresent=false \
-g jaxrs-cxf \
-o output
Steps to reproduce
  1. Generate Code from the example above with the cli command.
  2. Check the generated file src/gen/java/org/openapitools/model/Example.java in the output path

Expected result: The class supports additional properties. As described in #1466 this means it extends from HashMap
Actual result: The class has no support for additional properties.

Related issues/PRs

A similar issue was mentioned in #10306.

Without a fix for #1466 this might be less helpful, as the generated Code will not work as expected.

Suggest a fix

When checking for additionalProperties, the flag disallowAdditionalPropertiesIfNotPresent needs to be considered. Maybe ModelUtils#isMapSchema should return true in this case, as it does when additionalProperties is set explicitly.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the supplied openapi-generator CLI command and inspect the generated src/gen/java/org/openapitools/model/Example.java. Start with ModelUtils#isMapSchema and the jaxrs-cxf generation path, then verify that setting disallowAdditionalPropertiesIfNotPresent=false makes the model support additional properties, including extending HashMap as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.