liuxiran commented on issue #1039:
URL: 
https://github.com/apache/apisix-dashboard/issues/1039#issuecomment-770432807


   While researching this issue, I found a very cool library: 
`json-schema-faker`[1], an OSS with a large number of references and stars, 
used to generate fake data according to its `jsonshema`. I tested some of our 
plugins' schema, no matter which draft the schema is, the fake data can cover 
well.
   
   ### What the fake data looks like
   Take `api-breaker` for an example:
   * Jsonschema for api-breaker: 
https://github.com/apache/apisix/blob/09a4673a98269cb204c12a66980ff2f6f54abf39/apisix/plugins/api-breaker.lua#L30
   * the fake data generated by `json-schema-faker`: (configurable)
   ```json
   {
        "break_response_code": 200,
        "healthy": {
                "http_statuses": [200],
                "successes": 3
        },
        "max_breaker_sec": 300,
        "unhealthy": {
                "failures": 3,
                "http_statuses": [500]
        },
        "disable": false
   }
   ```
   ### How to add comments
   The fake data generated by `json-schema-faker` will be the `template` of the 
plugins. When enable a plugin, we could show this template instead of empty 
fields. 
   
   If we want to add comments for the `template`, we have to use `yaml` instead 
of `json`. 
   
   It's such a coincidence that there is an issue[2]  in `json-schema-faker` 
accurately matches our needs, I tried the method provided by the author, it 
worked very well
   * add comments for api-breaker: **please pay attention to `title` and 
`deacription` fields**
   ```json
   {
        "title": "plugin: api-breaker",
        "description": "some information about api-breaker",
        "$comment": "this is a mark for our injected plugin schema",
        "properties": {
                   ...
                "break_response_code": {
                           "title": "break_response_code title",
                        "maximum": 599,
                        "minimum": 200,
                        "type": "integer",
                        "description": "comment for break_response_code"
                },
                "healthy": {
                        "default": {
                                "http_statuses": [200],
                                "successes": 3
                        },
                        "properties": {
                                "http_statuses": {
                                        "default": [200],
                                        "items": {
                                                "maximum": 499,
                                                "minimum": 200,
                                                "type": "integer"
                                        },
                                        "minItems": 1,
                                        "type": "array",
                                        "uniqueItems": true,
                                        "description": "comment for 
http_statuses"
                                },
                                "successes": {
                                        "default": 3,
                                        "minimum": 1,
                                        "type": "integer",
                                        "description": "comment for successes"
                                }
                        },
                        "type": "object"
                },
                
                
        },
        "required": ["break_response_code"],
        "type": "object"
   }
   ```
   **the yaml data with comments**
   ```yaml
   ## plugin: api-breaker
   #
   # some information about api-breaker
   
   
   ## break_response_code title ##
   #
   # comment for break_response_code
   #
   # Minimum value: 200
   #
   # Maximum value: 599
   #
   break_response_code: 200
   
   ## healthy ##
   #
   # Default value: [object Object]
   #
   healthy:
     
     ## http_statuses ##
     #
     # comment for http_statuses
     #
     # Default value: 200
     #
     http_statuses:
       - 200
   
     ## successes ##
     #
     # comment for successes
     #
     # Default value: 3
     #
     # Minimum value: 1
     #
     successes: 3
   
   ## max_breaker_sec ##
   #
   # comment for max_breaker_sec
   #
   # Default value: 300
   #
   # Minimum value: 3
   #
   max_breaker_sec: 300
   
   ## unhealthy ##
   #
   # Default value: [object Object]
   #
   unhealthy:
     
     ## failures ##
     #
     # comment for unhealthy failures
     #
     # Default value: 3
     #
     # Minimum value: 1
     #
     failures: 3
   
     ## http_statuses ##
     #
     # comment for unhealthy http_statuses
     #
     # Default value: 500
     #
     http_statuses:
       - 500
   
   ## disable ##
   #
   # comment for disable
   #
   disable: false
   ```
   ### What we planed to do
   
   1. Import `json-schema-faker` to generated fake data when enable a plugin, 
so that we can have a default config data for plugin instead of an empty field.
   2. Add `title` `description` for each field of  each plugin, this will be 
the source of the fake data comments.
   3. Import the method of generated comments for fake data, and at the same 
time, we need to change the config data from `json` to `yaml`
   4. Supplement plugin instructions to increase the configuration of common 
scenarios
   
   Looking forward to your insights @juzhiyuan @LiteSun @imjoey
   
   reference:
   [1]: https://github.com/json-schema-faker/json-schema-faker
   [2]: https://github.com/json-schema-faker/json-schema-faker/issues/609


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to