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

[REQ][PHP] Add toPhpDefinition method to AbstractPhpCodegen #4288

Open
ybelenko opened this issue Oct 27, 2019 · 1 comment
Open

[REQ][PHP] Add toPhpDefinition method to AbstractPhpCodegen #4288

ybelenko opened this issue Oct 27, 2019 · 1 comment

Comments

@ybelenko
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I've spend some time under php mock server #3545
At first I analyzed current PHP client implementation. It uses flatten data type declarations, eg string[]. It would be enough for a Mock server, but it doesn't help with automatic request validation. Let's look at the example:

ImageUrls:
  type: array
  minItems: 1
  items:
    type: string
    format: uuid
    minLength: 5
    maxLength: 255

In current implementation this property will be described as:

/** @var string[] $imageUrls */
$imageUrls = [];

As a result we lost minItems, format, minLength and maxLength declarations. It makes automatic request validation impossible. It's obvious that we need all the props to check request under all requirements. The only way I see it we need to serialize all request parameters into PHP arrays. So the model above will look like:

$parameters = [
    'ImageUrls' => [
        'type' => 'array',
        'minItems' => 1,
        'items' => [
            'type' => 'string',
            'format' => 'uuid',
            'minLength' => 5,
            'maxLength' => 255,
        ],
    ],
];

Describe the solution you'd like

I think there should be method in AbstractPhpCodegen to convert Java Map/List instance into PHP array. Then I can convert parameters into PHP strings and add them to codegen, then use it in templates like $parameters = {{serializedParameters}};. Maybe this function/helper can be useful in other cases which I don't even realize yet.

Describe alternatives you've considered

If there is built-in Java method to convert any Java Map/List into PHP string definition please correct me.

cc @jebentier, @dkarlovi, @mandrean, @jfastnacht, @ackintosh, @renepardon

@ybelenko
Copy link
Contributor Author

ybelenko commented Nov 2, 2019

I found a possible solution. I can work with full model schema description in php with mustache:

    const SCHEMA = '{{{modelJson}}}';

    public static function mockModel()
    {
        $schema = json_decode(static::SCHEMA, true);
        echo json_encode($schema, JSON_PRETTY_PRINT);
        return $schema;
    }

Output php looks like:

class InlineObject
{
    
    /** @var int $id */
    private $id;
    
    /** @var string $name */
    private $name;
    
    /** @var string[] $imageUrls */
    private $imageUrls;
    
    /** @var \OpenAPIServer\Model\FoobarObjects[] $objects */
    private $objects;

    const SCHEMA = '{
  "required" : [ "ImageUrls", "Objects", "id", "name" ],
  "type" : "object",
  "properties" : {
    "id" : {
      "minimum" : 0,
      "type" : "integer",
      "format" : "int32"
    },
    "name" : {
      "maxLength" : 50,
      "minLength" : 3,
      "type" : "string"
    },
    "ImageUrls" : {
      "minItems" : 1,
      "type" : "array",
      "items" : {
        "maxLength" : 255,
        "minLength" : 5,
        "type" : "string",
        "format" : "uuid"
      }
    },
    "Objects" : {
      "minItems" : 1,
      "type" : "array",
      "items" : {
        "$ref" : "#/components/schemas/_foobar_Objects"
      }
    }
  }
}';

    public static function mockModel()
    {
        $schema = json_decode(static::SCHEMA, true);
        echo json_encode($schema, JSON_PRETTY_PRINT);
        return $schema;
    }
}

mockModel() call outputs:

{
    "required": [
        "ImageUrls",
        "Objects",
        "id",
        "name"
    ],
    "type": "object",
    "properties": {
        "id": {
            "minimum": 0,
            "type": "integer",
            "format": "int32"
        },
        "name": {
            "maxLength": 50,
            "minLength": 3,
            "type": "string"
        },
        "ImageUrls": {
            "minItems": 1,
            "type": "array",
            "items": {
                "maxLength": 255,
                "minLength": 5,
                "type": "string",
                "format": "uuid"
            }
        },
        "Objects": {
            "minItems": 1,
            "type": "array",
            "items": {
                "$ref": "#\/components\/schemas\/_foobar_Objects"
            }
        }
    }
}

This issue can be closed, but I want a feedback from PHP templates creators.
Do we need a function that converts Java map to valid PHP array string or not? Eg. {"foobar": "Hello World!"} transforms to ["foobar" => "Hello World!"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant