Skip to content

Commit

Permalink
Rename models and operations with mixed cases (#904)
Browse files Browse the repository at this point in the history
* Fix snake case issues

A few operations and models are affected by a small problem where a
character gets isolated. This fixes it.

* Fix api as well

* Use a constant

* Fix NTML

* Extract to JSON file

* Imports
  • Loading branch information
therve committed Mar 25, 2022
1 parent 83f2647 commit 7400fb8
Show file tree
Hide file tree
Showing 38 changed files with 165 additions and 149 deletions.
6 changes: 3 additions & 3 deletions .generator/src/generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def cli(input, output):
env.filters["parameter_schema"] = openapi.parameter_schema
env.filters["parameters"] = openapi.parameters
env.filters["return_type"] = openapi.return_type
env.filters["snake_case"] = formatter.snake_case
env.filters["safe_snake_case"] = openapi.safe_snake_case

env.globals["enumerate"] = enumerate
env.globals["version"] = version
Expand Down Expand Up @@ -88,7 +88,7 @@ def cli(input, output):
package.mkdir(exist_ok=True)

for name, model in models.items():
filename = formatter.snake_case(name) + ".py"
filename = openapi.safe_snake_case(name) + ".py"
model_path = package / "model" / filename
model_path.parent.mkdir(parents=True, exist_ok=True)
with model_path.open("w") as fp:
Expand All @@ -104,7 +104,7 @@ def cli(input, output):
fp.write(models_j2.render(models=sorted(models)))

for name, operations in apis.items():
filename = formatter.snake_case(name) + "_api.py"
filename = openapi.safe_snake_case(name) + "_api.py"
api_path = package / "api" / filename
api_path.parent.mkdir(parents=True, exist_ok=True)
with api_path.open("w") as fp:
Expand Down
10 changes: 10 additions & 0 deletions .generator/src/generator/openapi.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import json
import pathlib
import yaml
from jsonref import JsonRef
from yaml import CSafeLoader

from . import formatter

with (pathlib.Path(__file__).parent / "replacement.json").open() as f:
EDGE_CASES = json.load(f)


def load(filename):
path = pathlib.Path(filename)
Expand Down Expand Up @@ -286,6 +290,12 @@ def operation(spec, operation_id):
return None


def safe_snake_case(value):
for token, replacement in EDGE_CASES.items():
value = value.replace(token, replacement)
return formatter.snake_case(value)


def get_api_models(operations):
seen = set()
for _, _, operation in operations:
Expand Down
1 change: 1 addition & 0 deletions .generator/src/generator/replacement.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"IdP": "Idp", "AuthNMapping": "AuthnMapping", "AuthN ": "Authn ", "IoT": "Iot"}
14 changes: 7 additions & 7 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ from {{ package }}.model_utils import (
none_type,
)
{%- for model in get_api_models(operations) %}
from {{ package }}.{{ version }}.model.{{ model|snake_case }} import {{ model }}
from {{ package }}.{{ version }}.model.{{ model|safe_snake_case }} import {{ model }}
{%- endfor %}

{% set classname = name.replace(" ", "") + "Api" %}
Expand All @@ -22,14 +22,14 @@ class {{ classname }}:
{% for path, method, operation in operations|sort(attribute="2.operationId") %}
{%- set httpMethod = method.upper() %}
{%- set returnType = operation|return_type %}
self._{{ operation.operationId|snake_case }}_endpoint = _Endpoint(
self._{{ operation.operationId|safe_snake_case }}_endpoint = _Endpoint(
settings={
"response_type": {% if returnType %}({{ returnType }},){% else %}None{% endif %},
{%- set authMethods = operation.security if "security" in operation else openapi.security %}
"auth": [
{%- for auth in (authMethods or []) %}"{{ '", "'.join(auth.keys()) }}"{%- if not loop.last %}, {% endif %}{%- endfor %}],
"endpoint_path": "{{ path }}",
"operation_id": "{{ operation.operationId|snake_case }}",
"operation_id": "{{ operation.operationId|safe_snake_case }}",
"http_method": "{{httpMethod}}",
{%- set servers = operation.servers %}
{%- if servers %}
Expand Down Expand Up @@ -129,15 +129,15 @@ class {{ classname }}:
{% endfor %}

{%- for path, method, operation in operations|sort(attribute="2.operationId") %}
def {{ operation.operationId|snake_case }}(self, {% for name, parameter in operation|parameters if parameter.required %}{{name|attribute_name}}, {% endfor %}**kwargs):
def {{ operation.operationId|safe_snake_case }}(self, {% for name, parameter in operation|parameters if parameter.required %}{{name|attribute_name}}, {% endfor %}**kwargs):
"""{{ operation.summary|indent(8) }}.
{% if operation.description %}
{{ operation.description.replace("\\", "\\\\")|indent(8) }}
{% endif %}
This method makes a synchronous HTTP request by default. To make an
asynchronous HTTP request, please pass async_req=True.

>>> thread = api.{{ operation.operationId|snake_case }}(
>>> thread = api.{{ operation.operationId|safe_snake_case }}(
{%- for name, parameter in operation|parameters if parameter.required %}{{name|attribute_name}}, {% endfor %}async_req=True)
>>> result = thread.get()
{# keep new line #}
Expand Down Expand Up @@ -179,9 +179,9 @@ class {{ classname }}:
{%- set returnType = operation|return_type %}
:rtype: {% if returnType %}{{ returnType }}{% else %}None{% endif %}
"""
kwargs = self._{{ operation.operationId|snake_case }}_endpoint.default_arguments(kwargs)
kwargs = self._{{ operation.operationId|safe_snake_case }}_endpoint.default_arguments(kwargs)
{%- for name, parameter in operation|parameters if parameter.required %}
kwargs["{{ name|attribute_name }}"] = {{ name|attribute_name }}
{% endfor %}
return self._{{ operation.operationId|snake_case }}_endpoint.call_with_http_info(**kwargs)
return self._{{ operation.operationId|safe_snake_case }}_endpoint.call_with_http_info(**kwargs)
{% endfor %}
2 changes: 1 addition & 1 deletion .generator/src/generator/templates/apis.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{%- for api in apis %}
{%- set classname = api.replace(" ", "") + "Api" %}
from {{ package }}.{{ version }}.api.{{ classname|snake_case }} import {{ classname }}
from {{ package }}.{{ version }}.api.{{ classname|safe_snake_case }} import {{ classname }}
{%- endfor %}

2 changes: 1 addition & 1 deletion .generator/src/generator/templates/configuration.j2
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Configuration(BaseConfiguration):
{%- for operations in apis.values() %}
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
{%- if "x-unstable" in operation %}
"{{ operation.operationId|snake_case }}": False,
"{{ operation.operationId|safe_snake_case }}": False,
{%- endif %}
{%- endfor %}
{%- endfor %}
Expand Down
2 changes: 1 addition & 1 deletion .generator/src/generator/templates/model_generic.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{%- if refs %}
def lazy_import():
{%- for ref in refs %}
from {{ package }}.{{ version }}.model.{{ ref|snake_case }} import {{ ref }}
from {{ package }}.{{ version }}.model.{{ ref|safe_snake_case }} import {{ ref }}
{%- endfor %}
{# keep new line #}
{%- for ref in refs %}
Expand Down
2 changes: 1 addition & 1 deletion .generator/src/generator/templates/model_oneof.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{%- if refs %}
def lazy_import():
{%- for ref in refs %}
from {{ package }}.{{ version }}.model.{{ ref|snake_case }} import {{ ref }}
from {{ package }}.{{ version }}.model.{{ ref|safe_snake_case }} import {{ ref }}
{%- endfor %}
{%- for ref in refs %}
globals()["{{ ref }}"] = {{ ref }}
Expand Down
2 changes: 1 addition & 1 deletion .generator/src/generator/templates/model_simple.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{%- set ref = get_type_for_items(model) %}
{%- if ref %}
def lazy_import():
from {{ package }}.{{ version }}.model.{{ ref|snake_case }} import {{ ref }}
from {{ package }}.{{ version }}.model.{{ ref|safe_snake_case }} import {{ ref }}

globals()["{{ ref }}"] = {{ ref }}
{%- endif %}
Expand Down
2 changes: 1 addition & 1 deletion .generator/src/generator/templates/models.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{%- for model in models %}
from {{ package }}.{{ version }}.model.{{ model|snake_case }} import {{ model }}
from {{ package }}.{{ version }}.model.{{ model|safe_snake_case }} import {{ model }}
{%- endfor %}

12 changes: 6 additions & 6 deletions docs/datadog_api_client.v1.model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4769,18 +4769,18 @@ usage\_ingested\_spans\_response
:undoc-members:
:show-inheritance:

usage\_io\_t\_hour
------------------
usage\_iot\_hour
----------------

.. automodule:: datadog_api_client.v1.model.usage_io_t_hour
.. automodule:: datadog_api_client.v1.model.usage_iot_hour
:members:
:undoc-members:
:show-inheritance:

usage\_io\_t\_response
----------------------
usage\_iot\_response
--------------------

.. automodule:: datadog_api_client.v1.model.usage_io_t_response
.. automodule:: datadog_api_client.v1.model.usage_iot_response
:members:
:undoc-members:
:show-inheritance:
Expand Down
6 changes: 3 additions & 3 deletions docs/datadog_api_client.v2.api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ audit\_api
:undoc-members:
:show-inheritance:

auth\_n\_mappings\_api
----------------------
authn\_mappings\_api
--------------------

.. automodule:: datadog_api_client.v2.api.auth_n_mappings_api
.. automodule:: datadog_api_client.v2.api.authn_mappings_api
:members:
:undoc-members:
:show-inheritance:
Expand Down
96 changes: 48 additions & 48 deletions docs/datadog_api_client.v2.model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,130 +305,130 @@ audit\_logs\_warning
:undoc-members:
:show-inheritance:

auth\_n\_mapping
----------------
authn\_mapping
--------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping
.. automodule:: datadog_api_client.v2.model.authn_mapping
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_attributes
----------------------------
authn\_mapping\_attributes
--------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_attributes
.. automodule:: datadog_api_client.v2.model.authn_mapping_attributes
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_create\_attributes
------------------------------------
authn\_mapping\_create\_attributes
----------------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_create_attributes
.. automodule:: datadog_api_client.v2.model.authn_mapping_create_attributes
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_create\_data
------------------------------
authn\_mapping\_create\_data
----------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_create_data
.. automodule:: datadog_api_client.v2.model.authn_mapping_create_data
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_create\_relationships
---------------------------------------
authn\_mapping\_create\_relationships
-------------------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_create_relationships
.. automodule:: datadog_api_client.v2.model.authn_mapping_create_relationships
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_create\_request
---------------------------------
authn\_mapping\_create\_request
-------------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_create_request
.. automodule:: datadog_api_client.v2.model.authn_mapping_create_request
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_included
--------------------------
authn\_mapping\_included
------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_included
.. automodule:: datadog_api_client.v2.model.authn_mapping_included
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_relationships
-------------------------------
authn\_mapping\_relationships
-----------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_relationships
.. automodule:: datadog_api_client.v2.model.authn_mapping_relationships
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_response
--------------------------
authn\_mapping\_response
------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_response
.. automodule:: datadog_api_client.v2.model.authn_mapping_response
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_update\_attributes
------------------------------------
authn\_mapping\_update\_attributes
----------------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_update_attributes
.. automodule:: datadog_api_client.v2.model.authn_mapping_update_attributes
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_update\_data
------------------------------
authn\_mapping\_update\_data
----------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_update_data
.. automodule:: datadog_api_client.v2.model.authn_mapping_update_data
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_update\_relationships
---------------------------------------
authn\_mapping\_update\_relationships
-------------------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_update_relationships
.. automodule:: datadog_api_client.v2.model.authn_mapping_update_relationships
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mapping\_update\_request
---------------------------------
authn\_mapping\_update\_request
-------------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mapping_update_request
.. automodule:: datadog_api_client.v2.model.authn_mapping_update_request
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mappings\_response
---------------------------
authn\_mappings\_response
-------------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mappings_response
.. automodule:: datadog_api_client.v2.model.authn_mappings_response
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mappings\_sort
-----------------------
authn\_mappings\_sort
---------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mappings_sort
.. automodule:: datadog_api_client.v2.model.authn_mappings_sort
:members:
:undoc-members:
:show-inheritance:

auth\_n\_mappings\_type
-----------------------
authn\_mappings\_type
---------------------

.. automodule:: datadog_api_client.v2.model.auth_n_mappings_type
.. automodule:: datadog_api_client.v2.model.authn_mappings_type
:members:
:undoc-members:
:show-inheritance:
Expand Down
Loading

0 comments on commit 7400fb8

Please sign in to comment.