Find the oneOf schema that matches the input data (e.g. payload). If exactly one schema matches the input data, an instance of that schema is returned. If zero or more than one schema match the input data, an exception is raised. In OAS 3.x, the payload MUST, by validation, matc
(cls, model_kwargs, constant_kwargs, model_arg=None)
| 1848 | |
| 1849 | |
| 1850 | def get_oneof_instance(cls, model_kwargs, constant_kwargs, model_arg=None): |
| 1851 | """ |
| 1852 | Find the oneOf schema that matches the input data (e.g. payload). |
| 1853 | If exactly one schema matches the input data, an instance of that schema |
| 1854 | is returned. |
| 1855 | If zero or more than one schema match the input data, an exception is raised. |
| 1856 | In OAS 3.x, the payload MUST, by validation, match exactly one of the |
| 1857 | schemas described by oneOf. |
| 1858 | |
| 1859 | Args: |
| 1860 | cls: the class we are handling |
| 1861 | model_kwargs (dict): var_name to var_value |
| 1862 | The input data, e.g. the payload that must match a oneOf schema |
| 1863 | in the OpenAPI document. |
| 1864 | constant_kwargs (dict): var_name to var_value |
| 1865 | args that every model requires, including configuration, server |
| 1866 | and path to item. |
| 1867 | |
| 1868 | Kwargs: |
| 1869 | model_arg: (int, float, bool, str, date, datetime, ModelSimple, None): |
| 1870 | the value to assign to a primitive class or ModelSimple class |
| 1871 | Notes: |
| 1872 | - this is only passed in when oneOf includes types which are not object |
| 1873 | - None is used to suppress handling of model_arg, nullable models are handled in __new__ |
| 1874 | |
| 1875 | Returns |
| 1876 | oneof_instance (instance) |
| 1877 | """ |
| 1878 | if len(cls._composed_schemas["oneOf"]) == 0: |
| 1879 | return None |
| 1880 | |
| 1881 | oneof_instances = [] |
| 1882 | # Iterate over each oneOf schema and determine if the input data |
| 1883 | # matches the oneOf schemas. |
| 1884 | for oneof_class in cls._composed_schemas["oneOf"]: |
| 1885 | # The composed oneOf schema allows the 'null' type and the input data |
| 1886 | # is the null value. This is a OAS >= 3.1 feature. |
| 1887 | if oneof_class is none_type: |
| 1888 | # skip none_types because we are deserializing dict data. |
| 1889 | # none_type deserialization is handled in the __new__ method |
| 1890 | continue |
| 1891 | |
| 1892 | single_value_input = allows_single_value_input(oneof_class) |
| 1893 | |
| 1894 | try: |
| 1895 | if not single_value_input: |
| 1896 | if constant_kwargs.get("_spec_property_naming"): |
| 1897 | oneof_instance = oneof_class._from_openapi_data( |
| 1898 | **model_kwargs, **constant_kwargs |
| 1899 | ) |
| 1900 | else: |
| 1901 | oneof_instance = oneof_class(**model_kwargs, **constant_kwargs) |
| 1902 | else: |
| 1903 | if issubclass(oneof_class, ModelSimple): |
| 1904 | if constant_kwargs.get("_spec_property_naming"): |
| 1905 | oneof_instance = oneof_class._from_openapi_data( |
| 1906 | model_arg, **constant_kwargs |
| 1907 | ) |
no test coverage detected