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

Schema: defining multiple types #45

Open
happyDemon opened this issue Dec 22, 2020 · 4 comments
Open

Schema: defining multiple types #45

happyDemon opened this issue Dec 22, 2020 · 4 comments
Labels
bug Something isn't working

Comments

@happyDemon
Copy link

I have an internal API that interfaces with multiple third-party APIs and normalizes the response to something more manageable.

Because of this, I have properties that can either be 'integer' or 'string' depending on the service.

Following the spec: https://swagger.io/docs/specification/data-models/data-types/ (mixed types) it should be possible by defining them as OneOff.

Which kind of works, but I lose the possibility to set a description and such.

Is this something that just hasn't been implemented in this library or are you omitting it for a specific reason?

@matthew-inamdar
Copy link
Member

Hi @happyDemon 👋

This functionality is provided through:

Example usage will be:

OneOf::create()->schemas($schemaOne, $schemaTwo);

@matthew-inamdar matthew-inamdar added the question Further information is requested label Dec 23, 2020
@happyDemon
Copy link
Author

happyDemon commented Dec 23, 2020

Thanks for the quick reply, there's a little hiccup though, since they don't extend \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema I'm unable to assign them in quite a few places.

Assigning as a property on an object-type schema for example, or when trying to add it to the schema of a MediaType.

Currently, as a workaround, I've implemented the functionality I needed locally in a separate OneOfProperty class

@matthew-inamdar matthew-inamdar added bug Something isn't working and removed question Further information is requested labels Jan 3, 2021
@matthew-inamdar
Copy link
Member

@happyDemon you're right, I'll look at providing an update to fix this 👍

@Amegatron
Copy link

Greetings! I would also like to have this work out of the box. But meanwhile if anybody else is looking for workaround, it could be roughly done by adding a Schema-wrapper for SchemaComposition. Something like this:

<?php

declare(strict_types=1);

namespace SomeNameSpace\Goes\Here;

use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use GoldSpecDigital\ObjectOrientedOAS\Objects\SchemaComposition;

class SchemaCompositionWrapper extends Schema
{
    private SchemaComposition $composition;

    public function composition(SchemaComposition $composition): self
    {
        $instance = clone $this;
        $instance->composition = $composition;

        return $instance;
    }

    protected function generate(): array
    {
        return array_merge(
            parent::generate(),
            $this->composition->toArray(),
        );
    }
}

The usage is simple:

$schema = SchemaCompositionWrapper::create()
    ->composition(
        AnyOf::create()->schemas(...)
    );

$mediaType->schema($schema);

(As always, be cautious if you copy-paste this :) It's just a draft)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants