Formats JSON and YAML files containing OpenAPI schemas.
apigenie format [OPTIONS] FILES
This command formats JSON and YAML files containing OpenAPI schemas. This formatting helps to:
API Genie formatter works with all supported file formats. However, it is important to note that it’s not a generic JSON/YAML parser.
The formatter can process partially invalid schemas. It fails only when it
encounters JSON or YAML syntax error. This is different from commands
like lint
or diff
, which need valid OpenAPI schemas.
Currently, the formatter can change the following aspects of input schemas:
Formatter command can be configured to list properties of OpenAPI objects in specific order. This greatly helps to improve the readability of OpenAPI schemas.
Below are supported modes of ordering properties of schema objects:
Each mode can be useful in specific situations. For example, original mode
is useful when modifying existing schemas to avoid excessive changes.
Sorted mode is a natural order for elements like path items or shared
schemas. Finally, custom order allows users to define a specific order for
properties within schema objects, which can be beneficial for request/response
bodies and operations where placing summary
and description
properties at
the beginning improves readability.
Property mode can be configured
Example - Preserve original property order for all schema objects
commands:
format:
openapi:
property_order:
default: original
Example - Sort all path items within Paths
objects alphabetically
commands:
format:
openapi:
property_order:
paths: sorted
Example - Specify custom property order for Operation
objects
commands:
format:
openapi:
property_order:
operation:
# place summary and description at the beginning, to help
# understand operation's purpose
- summary
- description
- tags
- operationId
- externalDocs
- deprecated
- parameters
- requestBody
- responses
- callbacks
- security
- servers
Properties not explicitly listed in the configuration for a specific schema object type will be placed after the listed ones, sorted alphabetically.
Example
If the configuration for the Contact
object looks like:
commands:
format:
openapi:
property_order:
contact:
- name
- url
- email
and the actual contact data before formatting looks like:
openapi:
info:
contact:
x-team: awesome
name: You
email: you@example.com
url: your.site
x-audience: internal
x-stability: stable
...
formatting it will change it to:
openapi:
info:
contact:
name: You
url: your.site
email: you@example.com
x-audience: internal
x-team: awesome
x-stability: stable
...
Without options, formatter formats input file(-s) using default configuration. The resulting content is written back into input files, without changing their format.
apigenie format input1.yaml input2.json
Optional path to the configuration file. If not specified, the default configuration is used. For details, see the configuration section.
apigenie format --config api-style-guide.yaml input1.yaml input2.json
Optional path to the output file. If not specified, resulting content is written to the input file.
If this option is given, formatter processes only a single file at a time.
Note that the output format is determined by this file’s extension. So this can be used to convert between JSON and YAML representations.
apigenie format input.yaml --output output.json
Formatter stores its configuration under the #/commands/format/openapi
key in the
configuration file. See the global configuration
section to understand how this file is structured.
This key stores number of whitespace characters used to indent by one level.
This section stores custom property orders for OpenAPI schema objects. Valid
keys in this section is either default
or name of schema object written in
camelCase.
Sets the default property order. Valid values are sorted
and original
.
The default value is original
.
Defines property order for the OpenAPI object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
openapi:
- openapi
- info
- jsoSchemaDialect
- servers
- paths
- webhooks
- components
- security
- tags
- externalDocs
Defines property order for the Info object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
info:
- title
- summary
- description
- version
- termsOfService
- contact
- license
Defines property order for the Contact object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
contact:
- name
- url
- email
Defines property order for the License object.
Its value can be sorted
, original
or an array of property names in desired order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
license:
- name
- identifier
- url
Defines property order for the Server object.
Its value can be sorted
, original
or an array of property names in desired order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
server:
- url
- description
- variables
Defines property order for the Server Variable object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
serverVariable:
- description
- default
- enum
Defines property order for the Paths object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
paths: sorted
Defines property order for the Path Item object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
pathItem:
- summary
- description
- parameters
- get
- post
- put
- patch
- delete
- options
- head
- trace
- servers
Defines property order for the Operation object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
operation:
- tags
- summary
- operationId
- description
- externalDocs
- deprecated
- parameters
- requestBody
- responses
- callbacks
- security
- servers
Defines property order for the Components object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
components:
- schemas
- responses
- parameters
- requestBodies
- headers
- pathItems
- securitySchemes
- links
- callbacks
- examples
Defines property order for the External Documentation object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
externalDocumentation:
- description
- url
Defines property order for the Parameter object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
parameter:
- name
- in
- description
- required
- deprecated
- style
- allowEmptyValue
- explode
- allowReserved
- schema
- content
- example
- examples
Defines property order for the Request Body object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
requestBody:
- description
- required
- content
Defines property order for the Media Type object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
mediaType:
- schema
- encoding
- example
- examples
Defines property order for the Encoding object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
encoding:
- contentType
- style
- headers
- explode
- allowReserved
Defines property order for the Responses object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
responses: sorted
Defines property order for the Response object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
response:
- description
- headers
- content
- links
Defines property order for the Example object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
example:
- summary
- description
- value
- externalValue
Defines property order for the Link object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
link:
- description
- server
- operationRef
- operationId
- parameters
- requestBody
Defines property order for the Header object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
header:
- name
- description
- required
- deprecated
- allowEmptyValue
- explode
- allowReserved
- schema
- content
- example
- examples
Defines property order for the Tag object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
tag:
- name
- description
- externalDocs
Defines property order for the Reference object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
reference:
- $ref
- summary
- description
Defines property order for the Schema object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
schema:
- title
- description
- type
- format
- const
- enum
- minimum
- exclusiveMinimum
- maximum
- exclusiveMaximum
- multipleOf
- minLength
- maxLength
- pattern
- items
- prefixItemCount
- minItems
- maxItems
- uniqueItems
- required
- properties
- patternProperties
- propertyNames
- additionalProperties
- minProperties
- maxProperties
- allOf
- oneOf
- anyOf
- not
- discriminator
- xml
- externalDocs
Defines property order for the Discriminator object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
discriminator:
- propertyName
- mapping
Defines property order for the XML object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
xml:
- name
- namespace
- prefix
- attribute
- wrapped
Defines property order for the Security Scheme object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
securityScheme:
- name
- description
- type
- in
- scheme
- bearerFormat
- flows
- openIdConnectUrl
Defines property order for the OAuthFlows object.
Its value can be sorted
, original
or an array of property names in desired
order.
Example - Recommended property order for best readability
commands:
format:
openapi:
property_order:
oauthFlows:
- implicit
- password
- clientCredentials
- authorizationCode