|
24 | 24 | from rest_framework.views import APIView
|
25 | 25 | from rest_framework.viewsets import GenericViewSet, ModelViewSet
|
26 | 26 |
|
27 |
| -from .models import BasicModel, ForeignKeySource |
| 27 | +from .models import BasicModel, ForeignKeySource, ManyToManySource |
28 | 28 |
|
29 | 29 | factory = APIRequestFactory()
|
30 | 30 |
|
@@ -701,6 +701,51 @@ def test_schema_for_regular_views(self):
|
701 | 701 | assert schema == expected
|
702 | 702 |
|
703 | 703 |
|
| 704 | +class ManyToManySourceSerializer(serializers.ModelSerializer): |
| 705 | + class Meta: |
| 706 | + model = ManyToManySource |
| 707 | + fields = ('id', 'name', 'targets') |
| 708 | + |
| 709 | + |
| 710 | +class ManyToManySourceView(generics.CreateAPIView): |
| 711 | + queryset = ManyToManySource.objects.all() |
| 712 | + serializer_class = ManyToManySourceSerializer |
| 713 | + |
| 714 | + |
| 715 | +@unittest.skipUnless(coreapi, 'coreapi is not installed') |
| 716 | +class TestSchemaGeneratorWithManyToMany(TestCase): |
| 717 | + def setUp(self): |
| 718 | + self.patterns = [ |
| 719 | + url(r'^example/?$', ManyToManySourceView.as_view()), |
| 720 | + ] |
| 721 | + |
| 722 | + def test_schema_for_regular_views(self): |
| 723 | + """ |
| 724 | + Ensure that AutoField many to many fields are output as Integer. |
| 725 | + """ |
| 726 | + generator = SchemaGenerator(title='Example API', patterns=self.patterns) |
| 727 | + schema = generator.get_schema() |
| 728 | + |
| 729 | + expected = coreapi.Document( |
| 730 | + url='', |
| 731 | + title='Example API', |
| 732 | + content={ |
| 733 | + 'example': { |
| 734 | + 'create': coreapi.Link( |
| 735 | + url='/example/', |
| 736 | + action='post', |
| 737 | + encoding='application/json', |
| 738 | + fields=[ |
| 739 | + coreapi.Field('name', required=True, location='form', schema=coreschema.String(title='Name')), |
| 740 | + coreapi.Field('targets', required=True, location='form', schema=coreschema.Array(title='Targets', items=coreschema.Integer())), |
| 741 | + ] |
| 742 | + ) |
| 743 | + } |
| 744 | + } |
| 745 | + ) |
| 746 | + assert schema == expected |
| 747 | + |
| 748 | + |
704 | 749 | @unittest.skipUnless(coreapi, 'coreapi is not installed')
|
705 | 750 | class Test4605Regression(TestCase):
|
706 | 751 | def test_4605_regression(self):
|
|
0 commit comments