Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #35 from gtema/dev
Browse files Browse the repository at this point in the history
feat: diverse improvements of the network schemas
  • Loading branch information
gtema authored Mar 8, 2024
2 parents 6b63ea8 + 9280edd commit 88269de
Show file tree
Hide file tree
Showing 17 changed files with 844 additions and 171 deletions.
96 changes: 96 additions & 0 deletions codegenerator/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,102 @@ def find_response_schema(
return None


def get_operation_variants(spec: dict, operation_name: str):
"""Find operation body suitable for the generator"""
request_body = spec.get("requestBody")
# List of operation variants (based on the body)
operation_variants = []

if request_body:
content = request_body.get("content", {})
json_body_schema = content.get("application/json", {}).get("schema")
if json_body_schema:
mime_type = "application/json"
# response_def = json_body_schema
if "oneOf" in json_body_schema and "type" not in json_body_schema:
# There is a choice of bodies. It can be because of
# microversion or an action (or both)
# For action we should come here with operation_type="action" and operation_name must be the action name
# For microversions we build body as enum
# So now try to figure out what the discriminator is
discriminator = json_body_schema.get("x-openstack", {}).get(
"discriminator"
)
if discriminator == "microversion":
logging.debug("Microversion discriminator for bodies")
for variant in json_body_schema["oneOf"]:
variant_spec = variant.get("x-openstack", {})
operation_variants.append(
{"body": variant, "mime_type": mime_type}
)
# operation_variants.extend([{"body": x} for x in json_body_schema(["oneOf"])])
elif discriminator == "action":
# We are in the action. Need to find matching body
for variant in json_body_schema["oneOf"]:
variant_spec = variant.get("x-openstack", {})
if variant_spec.get("action-name") == operation_name:
discriminator = variant_spec.get("discriminator")
if (
"oneOf" in variant
and discriminator == "microversion"
):
logging.debug(
"Microversion discriminator for action bodies"
)
for subvariant in variant["oneOf"]:
subvariant_spec = subvariant.get(
"x-openstack", {}
)
operation_variants.append(
{
"body": subvariant,
"mode": "action",
"min-ver": subvariant_spec.get(
"min-ver"
),
"mime_type": mime_type,
}
)
else:
logging.debug(
"Action %s with %s", variant, discriminator
)
operation_variants.append(
{
"body": variant,
"mode": "action",
"min-ver": variant_spec.get("min-ver"),
"mime_type": mime_type,
}
)
break
if not operation_variants:
raise RuntimeError(
"Cannot find body specification for action %s"
% operation_name
)
else:
operation_variants.append(
{"body": json_body_schema, "mime_type": mime_type}
)
elif "application/octet-stream" in content:
mime_type = "application/octet-stream"
operation_variants.append({"mime_type": mime_type})
elif "application/openstack-images-v2.1-json-patch" in content:
mime_type = "application/openstack-images-v2.1-json-patch"
operation_variants.append({"mime_type": mime_type})
elif "application/json-patch+json" in content:
mime_type = "application/json-patch+json"
operation_variants.append({"mime_type": mime_type})
elif content == {}:
operation_variants.append({"body": None})
else:
# Explicitly register variant without body
operation_variants.append({"body": None})

return operation_variants


def get_resource_names_from_url(path: str):
"""Construct Resource name from the URL"""
path_elements = list(filter(None, path.split("/")))
Expand Down
111 changes: 16 additions & 95 deletions codegenerator/common/rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,22 @@ def discard_model(
logging.debug(f"Purging {ref} from models")
self.refs.pop(ref, None)

def is_operation_supporting_params(self) -> bool:
"""Determine whether operation supports any sort of parameters"""
if self.parameters:
return True
root = self.get_root_data_type()
if (
root
and isinstance(root, Struct)
and not root.fields
and not root.additional_fields_type
):
return False
elif root:
return True
return False


def sanitize_rust_docstrings(doc: str | None) -> str | None:
"""Sanitize the string to be a valid rust docstring"""
Expand All @@ -1038,98 +1054,3 @@ def sanitize_rust_docstrings(doc: str | None) -> str | None:
code_block_open = False
lines.append(line)
return "\n".join(lines)


def get_operation_variants(spec: dict, operation_name: str):
request_body = spec.get("requestBody")
# List of operation variants (based on the body)
operation_variants = []

if request_body:
content = request_body.get("content", {})
json_body_schema = content.get("application/json", {}).get("schema")
if json_body_schema:
mime_type = "application/json"
# response_def = json_body_schema
if "oneOf" in json_body_schema:
# There is a choice of bodies. It can be because of
# microversion or an action (or both)
# For action we should come here with operation_type="action" and operation_name must be the action name
# For microversions we build body as enum
# So now try to figure out what the discriminator is
discriminator = json_body_schema.get("x-openstack", {}).get(
"discriminator"
)
if discriminator == "microversion":
logging.debug("Microversion discriminator for bodies")
for variant in json_body_schema["oneOf"]:
variant_spec = variant.get("x-openstack", {})
operation_variants.append(
{"body": variant, "mime_type": mime_type}
)
# operation_variants.extend([{"body": x} for x in json_body_schema(["oneOf"])])
elif discriminator == "action":
# We are in the action. Need to find matching body
for variant in json_body_schema["oneOf"]:
variant_spec = variant.get("x-openstack", {})
if variant_spec.get("action-name") == operation_name:
discriminator = variant_spec.get("discriminator")
if (
"oneOf" in variant
and discriminator == "microversion"
):
logging.debug(
"Microversion discriminator for action bodies"
)
for subvariant in variant["oneOf"]:
subvariant_spec = subvariant.get(
"x-openstack", {}
)
operation_variants.append(
{
"body": subvariant,
"mode": "action",
"min-ver": subvariant_spec.get(
"min-ver"
),
"mime_type": mime_type,
}
)
else:
logging.debug(
"Action %s with %s", variant, discriminator
)
operation_variants.append(
{
"body": variant,
"mode": "action",
"min-ver": variant_spec.get("min-ver"),
"mime_type": mime_type,
}
)
break
if not operation_variants:
raise RuntimeError(
"Cannot find body specification for action %s"
% operation_name
)
else:
operation_variants.append(
{"body": json_body_schema, "mime_type": mime_type}
)
elif "application/octet-stream" in content:
mime_type = "application/octet-stream"
operation_variants.append({"mime_type": mime_type})
elif "application/openstack-images-v2.1-json-patch" in content:
mime_type = "application/openstack-images-v2.1-json-patch"
operation_variants.append({"mime_type": mime_type})
elif "application/json-patch+json" in content:
mime_type = "application/json-patch+json"
operation_variants.append({"mime_type": mime_type})
elif content == {}:
operation_variants.append({"body": None})
else:
# Explicitly register variant without body
operation_variants.append({"body": None})

return operation_variants
Loading

0 comments on commit 88269de

Please sign in to comment.