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] wrong interpretation of missing type in anyOf/allOf/oneOf as nullable #16466

Open
5 of 6 tasks
RoadRunnr opened this issue Aug 31, 2023 · 0 comments · May be fixed by #16467
Open
5 of 6 tasks

[BUG] wrong interpretation of missing type in anyOf/allOf/oneOf as nullable #16466

RoadRunnr opened this issue Aug 31, 2023 · 0 comments · May be fixed by #16467

Comments

@RoadRunnr
Copy link

RoadRunnr commented Aug 31, 2023

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

Since PR #15698, the generator assumes that a schema object without a type property means that the object is nullable. That assumption is as far as I can see not support by either OAS 3.0.3, OAS 3.1 or JSON Schema.

For OAS 3.0.3 the situation is explicitly documented at https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types, which states:

null is not supported as a type

For OAS 3.1 the null type is supported, however there is nothing in OAS nor JSON Schema that mandates that a missing type equals the null type.

openapi-generator version
  • 7.0.0
  • from master branch
OpenAPI declaration file content or url

A OpenAPI specification as input:

openapi: 3.0.1
info:
  version: 1.0.0
  title: Example
  license:
    name: MIT
servers:
  - url: http://api.example.xyz/v1
paths:
  /foo:
    get:
      operationId: list
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/AnyOfRequiredInObject"
components:
  schemas:
    AnyOfRequiredInObject:
      description: to test oneOf with required
      type: object
      oneOf:
        - required: [ field3 ]
        - required: [ field4 ]
      properties:
        field3:
          type: integer
        field4:
          type: integer

is translated in openapi generator to:

    "schemas" : {
      "AnyOfRequiredInObject" : {
        "description" : "to test oneOf with required",
        "nullable" : true,
        "oneOf" : [ ],
        "properties" : {
          "field3" : {
            "type" : "integer"
          },
          "field4" : {
            "type" : "integer"
          }
        },
        "type" : "object"
      }
    }

Note the missing declarations in oneOf and the erroneously added "nullable": true

Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g openapi -i test.yaml -o out
Steps to reproduce

run the openapi generator and check the output for missing oneOf and wrong nullable

Related issues/PRs
Suggest a fix

https://github.com/karzang/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java#L678-L696 should be changed to something like e.g.:

    private boolean isNullTypeSchema(Schema schema) {
        if (schema == null) {
            return false;
        }

        if ((schema.getType() == null) {
            return false;
        }

        if ((schema.getType().equals("null")) && schema.get$ref() == null) {
            return true;
        }

        // convert referenced enum of null only to `nullable:true`
        Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, schema);
        if (referencedSchema.getEnum() != null && referencedSchema.getEnum().size() == 1) {
            if ("null".equals(String.valueOf(referencedSchema.getEnum().get(0)))) {
                return true;
            }
        }

        return false;
    }
@RoadRunnr RoadRunnr linked a pull request Aug 31, 2023 that will close this issue
6 tasks
javiermtorres pushed a commit to travelping/openapi-generator that referenced this issue Sep 15, 2023
Not having a "type" property in a sub-schema is not the same as having
a `"type": null` property.

Using the YAML tag `null` in `"type"` and `"$ref"` properties to express a nullable
type is also not supported by either OAS or JSON Schema. `"type"` MUST be either a
string or an array of strings, `"$ref"` MUST be a string. The YAML tag `null` is
not the same as the string `"null"` and using it to convey the same meaning is not
supported by any of the specifications.

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

Successfully merging a pull request may close this issue.

1 participant