From 0a0457144c7ddb9bea34fdc28fc8ce1a37441301 Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Thu, 12 Dec 2024 15:23:49 -0500 Subject: [PATCH] Support oneOfs in response model --- linodecli/baked/request.py | 9 ++++++++- linodecli/baked/response.py | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/linodecli/baked/request.py b/linodecli/baked/request.py index 92fc03fbc..31a7da6c1 100644 --- a/linodecli/baked/request.py +++ b/linodecli/baked/request.py @@ -2,7 +2,7 @@ Request details for a CLI Operation """ -pass +import sys from openapi3.schemas import Schema @@ -138,6 +138,13 @@ def _parse_request_model(schema, prefix=None, parent=None, depth=0): """ args = [] + if depth > 0 and schema.oneOf is not None: + print( + f"WARN: {'.'.join(schema.path)}: " + f"Ignoring oneOf because it is not defined at the request schema's root level.", + file=sys.stderr, + ) + if schema.properties is None: return args diff --git a/linodecli/baked/response.py b/linodecli/baked/response.py index 323c3ddef..d5394d3a7 100644 --- a/linodecli/baked/response.py +++ b/linodecli/baked/response.py @@ -3,6 +3,7 @@ """ from openapi3.paths import MediaType +from openapi3.schemas import Schema def _is_paginated(response): @@ -169,10 +170,26 @@ def _parse_response_model(schema, prefix=None, nested_list_depth=0): ) attrs = [] - if schema.properties is None: + + properties = {} + + if schema.properties is not None: + properties.update(dict(schema.properties)) + + # We dynamically merge oneOf values here to ensure they are all accounted for + # in the response model. + if schema.oneOf is not None: + for entry in schema.oneOf: + entry_schema = Schema(schema.path, entry, schema._root) + if entry_schema.properties is None: + continue + + properties.update(dict(entry_schema.properties)) + + if properties is None: return attrs - for k, v in schema.properties.items(): + for k, v in properties.items(): pref = prefix + "." + k if prefix else k if v.type == "object":