swaggest / php-json-schema

High definition PHP structures with JSON-schema based validation
MIT License
438 stars 50 forks source link

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

Open ghost opened 2 years ago

ghost commented 2 years ago

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);