janephp icon indicating copy to clipboard operation
janephp copied to clipboard

OAS 3.1: Declaring type as array results in TypeError (for nullable types)

Open marcovtwout opened this issue 2 years ago • 5 comments

Jane version(s) affected: 7

Description
OpenAPI 3.1 is now fully compatible with JSON Schema 2020-12. In contrast to 3.0, the type keyword now allows for multiple types defined as an array. This commonly applies to nullable properties, but in general it could apply to any element where multiple types are allowed.

Attempting to generate client code containing a definition like this results in the following exception:

TypeError: Jane\Component\OpenApi3\JsonSchema\Model\Schema::setType(): Argument #1 ($type) must be of type ?string, array given, called in C:\example\vendor\jane-php\open-api-3\JsonSchema\Norma
lizer\SchemaNormalizer.php on line 174 in C:\example\vendor\jane-php\open-api-3\JsonSchema\Model\Schema.php on line 569

How to reproduce

  • Use any OAS 3.1 schema and create a property like this:
example_property:
  type:
  - string
  - 'null'
  • Attempt to generate client code.

Possible Solution
Jane-PHP should be able to deal with the official specification and allow arrays.

A workaround is to manually modify the schema and apply oneOf instead:

example_property:
  oneOf:
  - type: string
  - type: 'null'

Additional context

  • Migrating from 3.0 to 3.1: https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0#:~:text=Swap%20nullable%20for%20type%20arrays
  • Explanation of declaring null types in different OpenAPI versions: https://stackoverflow.com/a/48114322/729324
  • JsonSchema describing the type property as string or array: https://json-schema.org/understanding-json-schema/reference/type#:~:text=The%20type%20keyword%20may%20either%20be%20a%20string%20or%20an%20array%3A

marcovtwout avatar Oct 04 '23 09:10 marcovtwout

any update on this?

AlexOstrovsky avatar Feb 22 '24 09:02 AlexOstrovsky

+1

Fahl-Design avatar May 07 '24 16:05 Fahl-Design