knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.58k stars 280 forks source link

feat: write OpenAPI spec in json or yaml format #819

Closed nathanaelhoun closed 3 months ago

nathanaelhoun commented 3 months ago

From https://swagger.io/specification/,

An OpenAPI document that conforms to the OpenAPI Specification is itself a JSON object, which may be represented either in JSON or YAML format.

This PR adds a configuration toggle to output the OpenAPI document in the JSON format, which is useful to use it with the package nextapps-be/laravel-swagger-ui without the yaml PECL extension.

shalvah commented 3 months ago

I'm not in favour of this (I think JSON is a terrible format for this), but I'll accept this. However, you need to add it to the config reference and remove it from the default config file (there are too many options; only the most common and important stuff should be there).

nathanaelhoun commented 3 months ago

Thanks for your understanding. I agree that Yaml format is more readable, but allowing to publish as JSON would be a great enhancement for our deploiement setup.

I replaced the configuration for a more simple toggle scribe.openapi.json (as it can only be JSON or Yaml according to the OpenAPI spec), and removed it from the default config.


Having put more thought to it, I wonder if it wouldn't be simpler for both of us to add a composer script to my side to convert the yaml to json after building. This would avoid merging this new "niche" feature into Scribe. What do you think?

 "scripts": {
        "build-docs": [
            "@php artisan scribe:generate",
            "@php -r \"require 'vendor/autoload.php'; $spec = Symfony\\Component\\Yaml\\Yaml::parseFile('resources/swagger/openapi.yaml'); $json = json_encode($spec, JSON_PRETTY_PRINT); file_put_contents('resources/swagger/openapi.json', $json);\""
        ]
    },
nathanaelhoun commented 3 months ago

After some time and some thoughts about our usage of Scribe, we have decided to go with the composer script solution. Less software can be better software.

I'll let someone else re-submit this code if they are still interested in it.

Thanks again for making Scribe, it's a real gamechanger.

shalvah commented 3 months ago

Alright then! I'm still fine with adding it if it's hidden away.👍