-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support oneOfs & anyOfs in operation request & response schema (#701)
- Loading branch information
1 parent
defe7aa
commit d91e70b
Showing
10 changed files
with
331 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Provides various utility functions for use in baking logic. | ||
""" | ||
|
||
from collections import defaultdict | ||
from typing import Any, Dict, Set, Tuple | ||
|
||
from openapi3.schemas import Schema | ||
|
||
|
||
def _aggregate_schema_properties( | ||
schema: Schema, | ||
) -> Tuple[Dict[str, Any], Set[str]]: | ||
""" | ||
Aggregates all properties in the given schema, accounting properties | ||
nested in oneOf and anyOf blocks. | ||
:param schema: The schema to aggregate properties from. | ||
:return: The aggregated properties and a set containing the keys of required properties. | ||
""" | ||
|
||
schema_count = 0 | ||
properties = {} | ||
required = defaultdict(lambda: 0) | ||
|
||
def _handle_schema(_schema: Schema): | ||
if _schema.properties is None: | ||
return | ||
|
||
nonlocal schema_count | ||
schema_count += 1 | ||
|
||
properties.update(dict(_schema.properties)) | ||
|
||
# Aggregate required keys and their number of usages. | ||
if _schema.required is not None: | ||
for key in _schema.required: | ||
required[key] += 1 | ||
|
||
_handle_schema(schema) | ||
|
||
one_of = schema.oneOf or [] | ||
any_of = schema.anyOf or [] | ||
|
||
for entry in one_of + any_of: | ||
# pylint: disable=protected-access | ||
_handle_schema(Schema(schema.path, entry, schema._root)) | ||
|
||
return ( | ||
properties, | ||
# We only want to mark fields that are required by ALL subschema as required | ||
set(key for key, count in required.items() if count == schema_count), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: API Specification | ||
version: 1.0.0 | ||
servers: | ||
- url: http://localhost/v4 | ||
|
||
paths: | ||
/foo/bar: | ||
x-linode-cli-command: foo | ||
post: | ||
summary: Do something. | ||
operationId: fooBarPost | ||
description: This is description | ||
requestBody: | ||
description: Some description. | ||
required: True | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Foo' | ||
responses: | ||
'200': | ||
description: Successful response | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Foo' | ||
|
||
components: | ||
schemas: | ||
Foo: | ||
oneOf: | ||
- title: Usage 1 | ||
type: object | ||
required: | ||
- foobar | ||
- barfoo | ||
properties: | ||
foobar: | ||
type: string | ||
description: Some foobar. | ||
barfoo: | ||
type: integer | ||
description: Some barfoo. | ||
- title: Usage 2 | ||
type: object | ||
required: | ||
- foobar | ||
- foofoo | ||
properties: | ||
foobar: | ||
type: string | ||
description: Some foobar. | ||
foofoo: | ||
type: boolean | ||
description: Some foofoo. | ||
barbar: | ||
description: Some barbar. | ||
type: object | ||
anyOf: | ||
- type: object | ||
properties: | ||
foo: | ||
type: string | ||
description: Some foo. | ||
bar: | ||
type: integer | ||
description: Some bar. | ||
- type: object | ||
properties: | ||
baz: | ||
type: boolean | ||
description: Some baz. |
Oops, something went wrong.