Class OpenApiContract
java.lang.Object
com.irurueta.authserver.testsupport.OpenApiContract
Compares an OpenAPI contract under
apis/ with the OpenAPI document a running application serves. Both are
reduced to a sorted summary of what a client depends on: the info title and version, the security schemes, for
every operation under a path prefix its operation id, tags, security requirements, parameters, request body and
status codes, the schema of every media type an answer declares, and the properties and required properties of
every schema. Descriptions are left out: they are the contract's prose, rendered by springdoc from the annotations
generated from it. Two documents describe the same API when their summaries are equal, and assertEquals on
the two summaries lists exactly what drifted.
springdoc documents a few things the contract does not say: it repeats the return type of an operation under
every media type the operation produces and on answers declared without a body (such as a 401), and it
adds schemas of its own. restrictTo(SortedMap, SortedMap) leaves those out of the served summary; an
operation, a status code, a media type or a schema the contract declares and the application does not serve, or
serves differently, is still reported.
Documents are read with SnakeYAML's safe constructor, which reads YAML and JSON alike, and $refs to
#/components/... are resolved, so a contract may share parameters and responses that the served document
repeats inline.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringKey of the components.private static final StringKey of the content of a request body or a response.private static final StringInfix of the summary keys of the media types of an answer.private static final StringPrefix of a local reference.HTTP methods an OpenAPI path item may declare an operation for.private static final StringKey of a reference.private static final StringValue of the summary key of a status code: its body is described by the media type keys.private static final StringKey of a schema.private static final StringPrefix of the summary keys of the schemas.private static final StringKey of the security requirements. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateUtility class: not meant to be instantiated. -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringdescribeBody(Map<String, Object> body) Describes a request body.private static StringdescribeContent(Map<String, Object> content) Describes a content map by media type and schema.private static StringDescribes parameters by location, name and whether they are required.private static StringdescribeSchema(Map<String, Object> schema) Describes a schema by name, or by type for an inline one.private static StringdescribeSchemaProperties(Map<String, Object> schema) Describes the properties of a schema.private static StringdescribeScheme(Map<String, Object> scheme) Describes a security scheme by the fields a client relies on.private static StringdescribeSecurity(List<Object> requirements) Describes security requirements, an absent or empty list meaning a public operation.Reads a node as a list.Reads a node as a map.Parses an OpenAPI document.Reads an OpenAPI document from a file.private static ObjectResolves a local reference, such as#/components/responses/Unauthorized.Restricts the summary of a served document to the schemas and the media types of the answers the contract declares, leaving out what the server documents by itself.sorted(Collection<?> values) Sorts values by their text.Summarizes the API a document describes.private static voidsummarizePath(Map<String, Object> document, String path, Map<String, Object> item, Object globalSecurity, Map<String, String> summary) Summarizes every operation of a path.
-
Field Details
-
METHODS
HTTP methods an OpenAPI path item may declare an operation for. -
LOCAL_REF
Prefix of a local reference.- See Also:
-
REF
Key of a reference.- See Also:
-
SECURITY
Key of the security requirements.- See Also:
-
CONTENT
Key of the content of a request body or a response.- See Also:
-
SCHEMA
Key of a schema.- See Also:
-
COMPONENTS
Key of the components.- See Also:
-
SCHEMA_KEY
Prefix of the summary keys of the schemas.- See Also:
-
CONTENT_KEY
Infix of the summary keys of the media types of an answer.- See Also:
-
RESPONSE
Value of the summary key of a status code: its body is described by the media type keys.- See Also:
-
-
Constructor Details
-
OpenApiContract
private OpenApiContract()Utility class: not meant to be instantiated.
-
-
Method Details
-
read
Reads an OpenAPI document from a file.- Parameters:
file- YAML or JSON file.- Returns:
- the document.
- Throws:
IOException- if the file cannot be read.
-
parse
Parses an OpenAPI document.- Parameters:
text- YAML or JSON text.- Returns:
- the document.
-
summarize
Summarizes the API a document describes.- Parameters:
document- the OpenAPI document.pathPrefix- prefix of the paths that belong to the API, such as/api/.- Returns:
- the summary, sorted by key.
-
restrictTo
public static SortedMap<String,String> restrictTo(SortedMap<String, String> served, SortedMap<String, String> contract) Restricts the summary of a served document to the schemas and the media types of the answers the contract declares, leaving out what the server documents by itself.- Parameters:
served- the summary of the served document.contract- the summary of the contract.- Returns:
- the restricted summary.
-
summarizePath
private static void summarizePath(Map<String, Object> document, String path, Map<String, Object> item, Object globalSecurity, Map<String, String> summary) Summarizes every operation of a path.- Parameters:
document- the document, to resolve references.path- the path.item- the path item.globalSecurity- the security requirements of the document, used by operations that declare none.summary- the summary to fill.
-
describeScheme
Describes a security scheme by the fields a client relies on.- Parameters:
scheme- the scheme.- Returns:
- its description.
-
describeSchemaProperties
Describes the properties of a schema.- Parameters:
schema- the schema.- Returns:
- its sorted property names and required property names.
-
describeSecurity
Describes security requirements, an absent or empty list meaning a public operation.- Parameters:
requirements- the requirements.- Returns:
- their sorted description, such as
[bearer[admin]].
-
describeParameters
Describes parameters by location, name and whether they are required.- Parameters:
document- the document, to resolve references.parameters- the parameters.- Returns:
- their sorted description.
-
describeBody
Describes a request body.- Parameters:
body- the body, already resolved, empty when the operation takes none.- Returns:
- its description.
-
describeContent
Describes a content map by media type and schema.- Parameters:
content- the content map, empty when there is no body.- Returns:
- its sorted description.
-
describeSchema
Describes a schema by name, or by type for an inline one.- Parameters:
schema- the schema, empty when there is none.- Returns:
- its description, such as
ClientResponseorarray<ClientResponse>.
-
resolve
Resolves a local reference, such as#/components/responses/Unauthorized.- Parameters:
document- the document.node- the node, which may be a reference.- Returns:
- the node it refers to, or the node itself when it is not a reference.
-
sorted
Sorts values by their text.- Parameters:
values- the values.- Returns:
- a sorted list of their texts.
-
map
Reads a node as a map.- Parameters:
node- the node.- Returns:
- the node, or an empty map when it is absent or not a map.
-
list
Reads a node as a list.- Parameters:
node- the node.- Returns:
- the node, or an empty list when it is absent or not a list.
-