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

[Slim4] Add ref support to Data Mocker #4932

Merged
merged 11 commits into from
Jan 10, 2020

Conversation

ybelenko
Copy link
Contributor

@ybelenko ybelenko commented Jan 6, 2020

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

Current progress

Now OpenApiDataMocker supports refs in array items key and object properties. When mocker cannot find referenced model it throws InvalidArgumentException.

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

use OpenAPIServer\Mock\OpenApiDataMocker as Mocker;
$mocker = new Mocker();
$data = $mocker->mockFromSchema([
    'type' => 'object',
    'properties' => [
        'integer_from_1_to_100' => [
            'type' => 'integer',
            'minimum' => 1,
            'maximum' => 100,
        ],
        'float_from_minus3_to_3' => [
            'type' => 'number',
            'minimum' => -3,
            'maximum' => 3,
        ],
        'string_10chars' => [
            'type' => 'string',
            'minLength' => 10,
            'maxLength' => 10,
        ],
        'boolean' => [
            'type' => 'boolean',
        ],
        'array_of_strings' => [
            'type' => 'array',
            'items' => [
                'type' => 'string',
                'maxLength' => 20,
            ],
        ],
        'Object' => [
            'type' => 'object',
            'properties' => [
                'id' => [
                    'type' => 'integer',
                    'minimum' => 1,
                    'maximum' => 10
                ],
                'username' => [
                    'type' => 'string',
                    'maxLength' => 10,
                ],
            ],
        ],
        'Order_referenced_model' => [
            '$ref' => '#/components/schemas/Order',
        ],
    ],
]);

echo json_encode($data, JSON_PRETTY_PRINT);

output:

{
    "integer_from_1_to_100": 45,
    "float_from_minus3_to_3": -1.741,
    "string_10chars": "Lorem ipsu",
    "boolean": false,
    "array_of_strings": [
        "Lore"
    ],
    "Object": {
        "id": 3,
        "username": "L"
    },
    "Order_referenced_model": {
        "id": 1259632416,
        "petId": 350744286,
        "quantity": 331569927,
        "shipDate": "Lorem ipsum dolor sit amet, consectetur adipiscing el",
        "status": "approved",
        "complete": false
    }
}

TODO

Method mockFromRef should return model class instance(eg.new Animal()) not StdClass. As soon as Slim4 server has poor models implementation right now I will change it in future.

Related to #3545

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

@ybelenko
Copy link
Contributor Author

ybelenko commented Jan 7, 2020

Don't know what to do with these uncommitted changes:

diff --git a/samples/server/petstore/php-slim4/lib/Model/EnumClass.php b/samples/server/petstore/php-slim4/lib/Model/EnumClass.php
index 3ddd829b31..c3998d8602 100644
--- a/samples/server/petstore/php-slim4/lib/Model/EnumClass.php
+++ b/samples/server/petstore/php-slim4/lib/Model/EnumClass.php
@@ -30,8 +30,8 @@ class EnumClass implements ModelInterface
     private const MODEL_SCHEMA = <<<'SCHEMA'
 {
   "type" : "string",
-  "default" : "-efg",
-  "enum" : [ "_abc", "-efg", "(xyz)" ]
+  "enum" : [ "_abc", "-efg", "(xyz)" ],
+  "default" : "-efg"
 }
 SCHEMA;

I've rebased on latest master, made mvn clean package and it didn't help.

@wing328
Copy link
Member

wing328 commented Jan 10, 2020

CI failure not related to this PR except the sample update, which I will take care of after merging this PR into master.

@wing328 wing328 added this to the 4.2.3 milestone Jan 10, 2020
@wing328 wing328 merged commit 6dcdf5c into OpenAPITools:master Jan 10, 2020
@ybelenko ybelenko deleted the slim_data_mocker_schema branch January 10, 2020 08:44
@wing328
Copy link
Member

wing328 commented Jan 10, 2020

Don't know what to do with these uncommitted changes:

I'm able to repeat the issue but don't know what went wrong.

I've commented out php slim 4 in the ensure-up-to-date script for the time being.

jimschubert added a commit to jimschubert/openapi-generator that referenced this pull request Jan 11, 2020
* master: (187 commits)
  [core] Initial FeatureSet structures and definitions (OpenAPITools#3614)
  Add Cisco to the user list (OpenAPITools#4971)
  comment out php slim4 in ensure-up-to-date
  update samples
  [Python] Allow models to have properties of type self (OpenAPITools#4888)
  Add npmRepository option to javascript generators (OpenAPITools#4956)
  [Slim4] Add ref support to Data Mocker (OpenAPITools#4932)
  Fix auto-labeler for jax-rs (OpenAPITools#4943)
  [doc] full generator details (OpenAPITools#4941)
  comment out python flask 2 test (OpenAPITools#4949)
  [jaxrs-spec][quarkus] update to version 1.1.1.Final (OpenAPITools#4935)
  [cli] Full config help details (OpenAPITools#4928)
  Add RequestFile to typescript-node model template (OpenAPITools#4903)
  [csharp] enum suffix changes enumValueNameSuffix to enumValueSuffix (OpenAPITools#4927)
  [C#] allow customization of generated enum suffixes (OpenAPITools#4301)
  [Kotlin] Correct isInherited flag for Kotlin generators (OpenAPITools#4254)
  [Rust Server] Fix panic handling headers (OpenAPITools#4877)
  Initial CODEOWNERS (OpenAPITools#4924)
  [scala] Support for Set when array has uniqueItems=true (OpenAPITools#4926)
  remove nodejs server samples, scripts (OpenAPITools#4919)
  ...
@ybelenko ybelenko restored the slim_data_mocker_schema branch February 5, 2020 09:21
@ybelenko ybelenko deleted the slim_data_mocker_schema branch May 9, 2020 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants