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

JSON Schema to Open API 3.0 (Swagger) schema #120

Open
outcomer opened this issue Oct 20, 2022 · 1 comment
Open

JSON Schema to Open API 3.0 (Swagger) schema #120

outcomer opened this issue Oct 20, 2022 · 1 comment

Comments

@outcomer
Copy link

Hello!
I have general strategic question. This is not a bug or error.
Is it possible to add functionality to the package that will return a fully resolved schema, replacing all $refs and $injects and in it with real values, keeping schema structure?

Here is an example schema:

$schema = (object) [
	'type'                 => 'object',
	'properties'           => [
		'headers' => [
			'$ref'                 => "{$host}/src/AppBundle/Service/Validation/Definition/Headers/headers",
			'required'             => [
				'accept',
				'accept-language',
				'content-type',
				'x-client-hash',
			],
			'additionalProperties' => true,
		],
		'params'  => [
			'type'                 => 'object',
			'properties'           => [
				'XDEBUG_SESSION_START' => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/xdebugSessionStart"],
				'positionId'           => [
					'$ref'    => "{$host}/src/AppBundle/Service/Validation/Definition/Params/dbId",
					'default' => 0,
				],
				'firstName'            => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string"],
				'lastName'             => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string"],
				'email'                => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/email"],
				'phone'                => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/phone"],
				'message'              => [
					'$ref'    => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string",
					'default' => self::MSG_NOT_PROVIDED,
				],
				'marketingAgree'       => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/bool"],
				'personalAgree'        => ['$ref' => "{$host}/src/AppBundle/Service/Validation/Definition/Params/bool"],
				'utmSource'            => [
					'$ref'    => "{$host}/src/AppBundle/Service/Validation/Definition/Params/string",
					'default' => self::MSG_NOT_PROVIDED,
				],
			],
			'required'             => [
				'positionId',
				'firstName',
				'lastName',
				'email',
				'phone',
				'message',
				'marketingAgree',
				'personalAgree',
				'utmSource',
			],
			'additionalProperties' => false,
		],
	],
	'required'             => [
		'headers',
		'params',
	],
	'additionalProperties' => false,
];

One of $ref used here:

self::$schema = [
	'$schema'  => 'https://json-schema.org/draft/2020-12/schema',
	'$id'      => "{$host}/src/AppBundle/Service/Validation/Definition/Params/xdebugSessionStart",
	'$comment' => 'Parameter xdebug session start value.',
	'$slots'   => [
		'xdebugSessionStart-parameter' => [
			'type'   => ['string', 'null', 'boolean'],
			'$error' => [
				'type' => static::ERROR_TYPE,
			],
		],
	],
];

So XDEBUG_SESSION_START key from base schema should be after resolving:

'XDEBUG_SESSION_START' => [
	'type'   => [
		'string',
		'null',
		'boolean',
	],
	'$error' => ['type' => 'Provided value ({type}) must match the type: {expected}.'],
],

If $injects used for $slots xdebugSessionStart-parameter - then resolved schema must contain injected data.


Idea and motivation
If one day it became possible we can translate JSON Schema into Open API schema therefor having JSON Schema for each API route give you a chance to automattically build API docs for any route that was provided with validation schema

@outcomer outcomer changed the title JSON Schema to Oopen API (Swagger) schema JSON Schema to Open API 3.0 (Swagger) schema Oct 20, 2022
@razvanphp
Copy link

Are you aware that openapi 3.1 now has json schema 100% compatibility?

https://www.openapis.org/blog/2021/02/18/openapi-specification-3-1-released

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

No branches or pull requests

2 participants