php-solution / swagger-ui-gen
Extra functionality for generate Swagger specification
Installs: 77 761
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 11
Forks: 9
Open Issues: 5
Type:component
Requires
- php: ~7.1
- ext-json: *
- symfony/config: >=3.4
- symfony/console: >=3.4
- symfony/dependency-injection: >=3.4
- symfony/doctrine-bridge: >=3.4
- symfony/finder: >=3.4
- symfony/framework-bundle: >=3.4
- symfony/http-foundation: >=3.4
- symfony/routing: >=3.4
- symfony/templating: >=3.4
- symfony/validator: >=3.4
- symfony/yaml: >=3.4
Requires (Dev)
- phpunit/phpunit: ~6.0
- symfony/form: >=3.4
- symfony/var-dumper: >=3.4
Suggests
- doctrine/orm: For using Doctrine ORM OpenApi Schema Handler
- symfony/form: For using Symfony form type OpenApi Operation Handler
- symfony/serializer: For using Symfony serializer OpenApi Operation Handler
- symfony/validator: For using Symfony validator OpenApi Schema Handler
- v4.x-dev
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3
- v4.2
- v4.1.16
- v4.1.15
- v4.1.14
- v4.1.13
- v4.1.12
- v4.1.11
- v4.1.10
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- dev-master / 1.x-dev
- v0.2.x-dev
- v0.2.3.x-dev
- v0.2.3
- v0.2.2
- v0.2.1.x-dev
- v0.2.1
- v0.2
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1
- dev-openapi-3.0
- dev-feature/DI-142-Add-render-of-swagger-doc
- dev-security_annotation
- dev-json_body
This package is auto-updated.
Last update: 2024-11-06 03:23:38 UTC
README
At now this lib is under development.
SwaggerUIGenerator
This lib includes components and Symfony bundle for generate OpenApi specification.
Install
Via Composer
$ composer require php-solution/swagger-ui-gen
Integration to Project
-
Copy to web document root swagger-ui dist files
-
Copy to /web/assets/swagger
- index.html
- swagger-ui.css
- swagger-ui.js
- swagger-ui-bundle.js
- swagger-ui-standalone-preset.js
-
Change on index.html url to swagger openapi specification
const ui = SwaggerUIBundle({
url: "http://localhost/assets/swagger-spec.json",
})
Add Symfony Configuration
swagger_ui_gen:
options_provider:
defaults:
- '%kernel.root_dir%/Resources/swagger-doc/defaults.yml'
files:
- '%kernel.root_dir%/Resources/swagger-doc/general.yml'
- '%kernel.root_dir%/Resources/swagger-doc/tags.yml'
- '%kernel.root_dir%/Resources/swagger-doc/paths.yml'
- '%kernel.root_dir%/Resources/swagger-doc/security_def.yml'
- '%kernel.root_dir%/Resources/swagger-doc/definitions.yml'
folders:
- '@ProjectAdminBundle/Resources/config/swagger-doc'
handlers:
validator: false
form: false
form_validator: false
serializer: false
doctrine_orm: false
naming_strategy_service: 'PhpSolution\SwaggerUIGen\Bundle\ModelHandler\PropertyNaming\UnderscoreNamingStrategy'
Generate file with Swagger Specification
php bin\console swagger-gen:generate-spec --path=./web/assets/swagger-spec.json
Examples
Symfony project
See example of Symfony app files on /examples/project.
Debug mode for Symfony
- Add route to your general route.yml
_swagger_ui_gen: resource: '@SwaggerUIGenBundle/Resources/config/routing.yml'
- Add to swagger ui bootstrap html file(index.html):
<script>
window.onload = function() {
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
const debugDataUrl = getUrlParameter('url');
const ui = SwaggerUIBundle({
url: debugDataUrl ? debugDataUrl : "./data.json",
...
</script>
- Use on browser:
http://localhost/path-to-swagger-ui-bootstrap-html.html?url=/swagger-ui-gen/data.json
- See dumped data
http://localhost/swagger-ui-gen/dump
Example of symfony route specification
sf_route_paths: - route: 'get_list_of_entity' # symfony route name, required tags: ['admin'] # openapi tags, not required, default value: [] schemes: [] # openapi schemes, not required, default value: [] response: # use for openapi operation response, not required, default value: [] status_code: 200 # not required, default value: "default" type: 'object' # not required, default value: "object", complex values: ['array', 'object', 'collection'] mapping: type: ['doctrine', 'serializer'] # not required, use all if builders empty class: 'Project\AdminBundle\Lib\PaginatedCollection' # required serializer_groups: ['list'] # use only for serializer properties: items: # property name, require type: 'collection' mapping: {class: 'Project\AdminBundle\Entity\Admin'} openapi_params: # not required, default value: [] get: summary: 'Get admin list' responses: << : *defaultResponceErrors security: - api_key: [] - route: 'post_with_form_dto' tags: ['admin'] request: form_class: 'Project\AdminBundle\Form\Type\AdminCreateType' form_options: {validation_groups: ['create']} validation_class: 'Project\AdminBundle\Lib\AdminModel' validation_groups: ['create'] response: type: 'object' mapping: type: ['doctrine', 'serializer'] class: 'Project\AdminBundle\Entity\Admin' serializer_groups: ['get'] - route: 'post_with_form_multiple_dto' tags: ['admin'] request: form_class: 'Project\AdminBundle\Form\Type\MultipleAdminType' in: 'body' # use this option if you want to send data as json response: type: 'collection' openapi_params: {$ref: '#/definitions/Admin'}
Example of definition specification
sf_object_definitions: - name: 'Admin' mapping: type: ['doctrine', 'serializer'] class: 'Project\AdminBundle\Entity\Admin' - name: 'AdminCreate' type: 'object' mapping: type: ['validator'] validation_groups: ['create'] class: 'Project\AdminBundle\Lib\AdminListModel' # Some DTO properties: admins: type: 'collection' mapping: {class: 'Project\AdminBundle\Lib\AdminModel'} openapi_params: properties: email: {uniqueItems: true}
TODO:
- Use SF Configuration component as normalizer for DefinitionsBuilder, RoutePaths
- JMSSerializer Schema builder
- Tests