cebe / php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
MIT License
464 stars 87 forks source link

Add a way to create schemas #159

Open Herrick19 opened 2 years ago

Herrick19 commented 2 years ago

Hi,

The library allows to create paths, but apparently doesn't provide any way to add schemas.

Doing something like: $objOpenapi->components->schemas['foo'] = new Schema([ 'type' => Type::INTEGER, ]);

gives a warning and the element is not created: PHP Notice: Indirect modification of overloaded property cebe\openapi\spec\Components::$schemas has no effect in G:\test.php on line 119

Am I missing something ?

If not, it would be nice if we could create dynamically elements using this library.

Thank in advance

cebe commented 2 years ago

You can currently do it like this:


$schemas = []; // or $schemas = $objOpenapi->components->schemas;

$schemas['foo'] = new Schema([ 'type' => Type::INTEGER, ]);
// add more here ...

$objOpenapi->components->schemas = $schemas;

This library was initally created to only read OpenAPI files, writing was added later so the programming API is not optimal.