def test_deserialize_oneof_reference(self):
        """
        Validate the scenario when the type of a OAS property is 'oneOf', and the 'oneOf'
        schema is specified as a reference ($ref), not an inline 'oneOf' schema.
        """
        isosceles_triangle = petstore_api.Shape(
            shape_type="Triangle",
            triangle_type="IsoscelesTriangle"
        )
        assert isinstance(isosceles_triangle, petstore_api.IsoscelesTriangle)
        inst = petstore_api.Drawing(
            # 'main_shape' has type 'Shape', which is a oneOf [triangle, quadrilateral]
            # composed schema. So we should be able to assign a petstore_api.Triangle
            # to a 'main_shape'.
            main_shape=isosceles_triangle,
            shapes=[
                petstore_api.Shape(
                    shape_type="Triangle",
                    triangle_type="EquilateralTriangle"
                ),
                petstore_api.Triangle(
                    shape_type="Triangle",
                    triangle_type="IsoscelesTriangle"
                ),
                petstore_api.EquilateralTriangle(
                    shape_type="Triangle",
                    triangle_type="EquilateralTriangle"
                ),
                petstore_api.Shape(
                    shape_type="Quadrilateral",
                    quadrilateral_type="ComplexQuadrilateral"
                ),
            ],
        )
        assert isinstance(inst, petstore_api.Drawing)
        assert isinstance(inst.main_shape, petstore_api.IsoscelesTriangle)
        self.assertEqual(len(inst.shapes), 4)
        assert isinstance(inst.shapes[0], petstore_api.EquilateralTriangle)
        assert isinstance(inst.shapes[1], petstore_api.IsoscelesTriangle)
        assert isinstance(inst.shapes[2], petstore_api.EquilateralTriangle)
        assert isinstance(inst.shapes[3], petstore_api.ComplexQuadrilateral)

        # Validate we cannot assign the None value to main_shape because the 'null' type
        # is not one of the allowed types in the 'Shape' schema.
        err_msg = ("Invalid type for variable '{}'. "
            "Required value type is {} and passed type was {} at {}")
        with self.assertRaisesRegexp(
            petstore_api.ApiTypeError,
            err_msg.format("main_shape", "Shape", "NoneType", "\['main_shape'\]")
        ):
            inst = petstore_api.Drawing(
                # 'main_shape' has type 'Shape', which is a oneOf [triangle, quadrilateral]
                # So the None value should not be allowed and an exception should be raised.
                main_shape=None,
            )
Example #2
0
    def testShape(self):
        """Test Shape"""
        equilateral_triangle = petstore_api.Triangle(
            shape_type="Triangle",
            triangle_type="EquilateralTriangle"
        )
        assert isinstance(equilateral_triangle, petstore_api.EquilateralTriangle)

        isosceles_triangle = petstore_api.Triangle(
            shape_type="Triangle",
            triangle_type="IsoscelesTriangle"
        )
        assert isinstance(isosceles_triangle, petstore_api.IsoscelesTriangle)

        scalene_triangle = petstore_api.Triangle(
            shape_type="Triangle",
            triangle_type="ScaleneTriangle"
        )
        assert isinstance(scalene_triangle, petstore_api.ScaleneTriangle)

        complex_quadrilateral = petstore_api.Shape(
            shape_type="Quadrilateral",
            quadrilateral_type="ComplexQuadrilateral"
        )
        assert isinstance(complex_quadrilateral, petstore_api.ComplexQuadrilateral)

        simple_quadrilateral = petstore_api.Shape(
            shape_type="Quadrilateral",
            quadrilateral_type="SimpleQuadrilateral"
        )
        assert isinstance(simple_quadrilateral, petstore_api.SimpleQuadrilateral)

        # invalid shape_type (first discriminator)
        err_msg = ("Cannot deserialize input data due to invalid discriminator "
            "value. The OpenAPI document has no mapping for discriminator "
            "property '{}'='{}' at path: ()"
        )
        with self.assertRaisesRegexp(
            petstore_api.ApiValueError,
            err_msg.format("shapeType", "Circle")
        ):
            petstore_api.Shape(shape_type="Circle")

        # invalid quadrilateral_type (second discriminator)
        with self.assertRaisesRegexp(
            petstore_api.ApiValueError,
            err_msg.format("quadrilateralType", "Triangle")
        ):
            petstore_api.Shape(
                shape_type="Quadrilateral",
                quadrilateral_type="Triangle"
            )
    def test_create_instances(self):
        """
        Validate instance can be created using pythonic name or OAS names.
        """

        # Validate object can be created using pythonic names.
        inst = petstore_api.Shape(
            shape_type="Triangle",
            triangle_type="IsoscelesTriangle"
        )
        assert isinstance(inst, petstore_api.IsoscelesTriangle)

        # Validate object can be created using OAS names.
        # For example, this can be used to construct objects on the client
        # when the input data is available as JSON documents.
        data =  {
              'shapeType': "Triangle",
              'triangleType': "IsoscelesTriangle"
        }
        inst = petstore_api.Shape(_spec_property_naming=True, **data)
        assert isinstance(inst, petstore_api.IsoscelesTriangle)
Example #4
0
 def test_deserialize_oneof_reference(self):
     isosceles_triangle = petstore_api.Shape(
         shape_type="Triangle",
         triangle_type="IsoscelesTriangle"
     )
     assert isinstance(isosceles_triangle, petstore_api.IsoscelesTriangle)
     inst = petstore_api.Drawing(
         # 'main_shape' has type 'Shape', which is a oneOf [triangle, quadrilateral]
         # composed schema. So we should be able to assign a petstore_api.Triangle
         # to a 'main_shape'.
         main_shape=isosceles_triangle,
         shapes=[
             petstore_api.Shape(
                 shape_type="Triangle",
                 triangle_type="EquilateralTriangle"
             ),
             petstore_api.Triangle(
                 shape_type="Triangle",
                 triangle_type="IsoscelesTriangle"
             ),
             petstore_api.EquilateralTriangle(
                 shape_type="Triangle",
                 triangle_type="EquilateralTriangle"
             ),
             petstore_api.Shape(
                 shape_type="Quadrilateral",
                 quadrilateral_type="ComplexQuadrilateral"
             ),
         ],
     )
     assert isinstance(inst, petstore_api.Drawing)
     assert isinstance(inst.main_shape, petstore_api.IsoscelesTriangle)
     self.assertEqual(len(inst.shapes), 4)
     assert isinstance(inst.shapes[0], petstore_api.EquilateralTriangle)
     assert isinstance(inst.shapes[1], petstore_api.IsoscelesTriangle)
     assert isinstance(inst.shapes[2], petstore_api.EquilateralTriangle)
     assert isinstance(inst.shapes[3], petstore_api.ComplexQuadrilateral)