简体   繁体   English

使用 django 和 drf-yasg 重用序列化程序的问题

[英]Problem reusing serializers with django and drf-yasg

I am using django, django drf and drf-yasg to generate write my BE and generate documentation.我正在使用 django、django drf 和 drf-yasg 来生成编写我的 BE 并生成文档。

I have a model called User and a serializer for a user:我有一个名为 User 的模型和一个用于用户的序列化程序:

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = users_models.User
        fields = (users_models.User.first_name.field_name,
                  users_models.User.last_name.field_name,)

and I have some method Foo which gets two users.我有一些方法 Foo 可以获得两个用户。 This is what a serializer for the request looks like:这是请求的序列化程序的样子:

class FooRequestSerializer(serializers.ModelSerializer):
      first_user = UserSerializer(help_text="first user")
      second_user = UserSerializer(help_text="second user")

when I generate a swagger json scheme for this I view the json and the redoc I see that:当我为此生成一个swagger json方案时,我查看了json和redoc,我看到了:

  1. first_user and second_user have the same ref name ($ref) first_user 和 second_user 具有相同的引用名称 ($ref)
  2. The description of the second_user and the redoc reads "first user" and not second user. second_user 和 redoc 的描述为“第一个用户”而不是第二个用户。 this is because the description is taken from the $ref which has the first user description.这是因为描述取自具有第一个用户描述的 $ref。

I noted that if I make sure the ref names are distinct then the the redoc reads fine since first_user and second_user get their own description.我注意到如果我确保 ref 名称是不同的,那么 redoc 读起来很好,因为 first_user 和 second_user 得到了他们自己的描述。 The problem comes since I also want to be able to later use swagger codegen to create Java stubs, so the solution and from what I understand there is a different class for each distinct ref name.问题来了,因为我也希望以后能够使用 swagger codegen 来创建 Java 存根,所以解决方案和据我所知,每个不同的 ref 名称都有一个不同的类。 Ideally I would see that the call to foo is something like理想情况下,我会看到对 foo 的调用类似于

Foo(User first_user, User second_user)

This gets me to the problem that:这让我想到了一个问题:

  • If first_user and second_user has the same ref name then the redoc reads wrong and the second user has the first user description.如果 first_user 和 second_user 具有相同的引用名称,则 redoc 读取错误并且第二个用户具有第一个用户描述。
  • If first_user and second_user has distinct ref names then the redoc works but I get two disticnt classes generated, say如果 first_user 和 second_user 具有不同的 ref 名称,那么 redoc 可以工作,但我会生成两个不同的类,例如

    Foo(FirstUser first_user, SecondUser second_user) Foo(FirstUser first_user, SecondUser second_user)

What do I need to to inorder to have both working redoc and generated classes as expected ?我需要做什么才能按预期工作重文档和生成的类?

According to drf-yasg docs here , you can specify a ref_name in a Meta class of serializer.根据此处的drf-yasg 文档,您可以在序列化程序的 Meta 类中指定 ref_name 。 Therefore, something like this should work (although not very clean solution):因此,这样的事情应该工作(虽然不是很干净的解决方案):

class UserSerializer(serializers.ModelSerializer):

    class Meta:
        model = users_models.User
        fields = (users_models.User.first_name.field_name,
                  users_models.User.last_name.field_name,)


class FirstUserSerializer(UserSerializer):
    class Meta:
        ref_name = "User 1"


class SecondUserSerializer(UserSerializer):
    class Meta:
        ref_name = "User 2"


class FooRequestSerializer(serializers.ModelSerializer):
      first_user = FirstUserSerializer(help_text="first user")
      second_user = SecondUserSerializer(help_text="second user")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM