]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
utils: modelling: json_schema: replace underscore with dash in field name
authorAleš <ales.mrazek@nic.cz>
Wed, 2 Feb 2022 23:08:47 +0000 (00:08 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:54 +0000 (16:17 +0200)
manager/knot_resolver_manager/utils/modelling.py

index ead9b18c163d55cd4094d0ff6faf894bb3785808..3299e52822f5664471b991049982c326feff82c0 100644 (file)
@@ -107,22 +107,23 @@ def _get_properties_schema(typ: Type[Any]) -> Dict[Any, Any]:
     annot = typ.__dict__.get("__annotations__", {})
     docstring: str = typ.__dict__.get("__doc__", "") or ""
     attribute_documentation = _parse_attrs_docstrings(docstring)
-    for name, python_type in annot.items():
+    for field_name, python_type in annot.items():
+        name = field_name.replace("_", "-")
         schema[name] = _describe_type(python_type)
 
         # description
         if attribute_documentation is not None:
-            if name not in attribute_documentation:
-                raise SchemaException(f"The docstring does not describe field '{name}'", str(typ))
-            schema[name]["description"] = attribute_documentation[name]
-            del attribute_documentation[name]
+            if field_name not in attribute_documentation:
+                raise SchemaException(f"The docstring does not describe field '{field_name}'", str(typ))
+            schema[name]["description"] = attribute_documentation[field_name]
+            del attribute_documentation[field_name]
 
         # default value
-        if hasattr(typ, name):
+        if hasattr(typ, field_name):
             assert Serializable.is_serializable(
                 python_type
             ), f"Type '{python_type}' does not appear to be JSON serializable"
-            schema[name]["default"] = Serializable.serialize(getattr(typ, name))
+            schema[name]["default"] = Serializable.serialize(getattr(typ, field_name))
 
     if attribute_documentation is not None and len(attribute_documentation) > 0:
         raise SchemaException(