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

Is it possible to deactivate $ref in the output? #138

Open
ghost opened this issue Dec 7, 2021 · 0 comments
Open

Is it possible to deactivate $ref in the output? #138

ghost opened this issue Dec 7, 2021 · 0 comments

Comments

@ghost
Copy link

ghost commented Dec 7, 2021

We have classes with oneOf and discriminator (type attribute). When exporting an object there are "$ref" in the output which is unwanted for us. Question would be if that could be deactivated, if that should be like that or if we are using the library in a wrong way. We are not seeing "$ref" if we input stdClasses that do not extend ClassStructure.

I have attached a minimal example that shows the behaviour. I'm happy to answer more questions.

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Swaggest\JsonSchema\Schema;
use Swaggest\JsonSchema\Structure\ClassStructure;

class Order extends ClassStructure {
    public $items;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->items               = Schema::arr();
        $properties->items->items        = new Schema();
		$properties->items->items->oneOf = [
			AOrderItem::schema(),
			BOrderItem::schema(),
		];
        $ownerSchema->type = 'object';
        $ownerSchema->required = [
            self::names()->items
        ];
    }
}

class OrderItem extends ClassStructure {
    public $type;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->type = Schema::string();
        $ownerSchema->type = 'object';
        $ownerSchema->required = [
            self::names()->type
        ];
    }
}

class AOrderItem extends OrderItem {
    public $typeSpecific;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        parent::setUpProperties($properties, $ownerSchema);
        $properties->typeSpecific = ATypeSpecific::schema();
        $ownerSchema->required[] = self::names()->typeSpecific;
    }
}

class BOrderItem extends OrderItem {
    public $typeSpecific;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        parent::setUpProperties($properties, $ownerSchema);
        $properties->typeSpecific = BTypeSpecific::schema();
        $ownerSchema->required[] = self::names()->typeSpecific;
    }
}

class ATypeSpecific extends ClassStructure {
    public $a;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->a = Schema::string();
        $ownerSchema->type = 'object';
        $ownerSchema->required = [
            self::names()->a
        ];
    }
}

class BTypeSpecific extends ClassStructure {
    public $b;

    /**
     * @param Properties|static $properties
     * @param Schema $ownerSchema
     */
    public static function setUpProperties($properties, Schema $ownerSchema)
    {
        $properties->b = Schema::integer();
        $ownerSchema->type = 'object';
        $ownerSchema->required = [
            self::names()->b
        ];
    }
}

$ts = new BTypeSpecific();
$ts->b = 1;
$b = new BOrderItem();
$b->type = 'b';
$b->typeSpecific = $ts;
$order = new Order();
$order->items = [$b];

echo json_encode(Order::export($order), JSON_PRETTY_PRINT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants