Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapped adapter does not wrap the post payload in the given path when serializing #74

Open
prashand opened this issue May 2, 2019 · 1 comment

Comments

@prashand
Copy link

prashand commented May 2, 2019

Wrapped adapter is extremely convenient when deserializing unnecessarily wrapped json responses without having to create wrapper classes.

But it doesn't seem to work when serializing POST data.

Endpoint: POST api/{surveyId}/answers
Example Body:

{                                        //SurveyAnswerPayload.class
  "answers": [
    {                                     //SurveyAnswer.class
      "questionId": "1",
      "answer": "Foo"		
    }, {
      "questionId": "2",
      "answer": "Bar"
    }
  ]
}

Trying to use Wrapped adapter to avoid creating a wrapper object SurveyAnswerPayload like so:

public class SurveyAnswer {
    @Json(name = "questionId")
    public String questionId;

    @Json(name = "answer")
    public String answer;
}

public interface SurveyService{
  @POST("surveys/{surveyId}/answers")
  @Wrapped(path = {"answers"})
  Call<Void> postAnswers(@Path("surveyId") String surveyId, @Body List<SurveyAnswer> answers);
}

Sadly this doesn't work. It serializes into [{"questionId":"1","answer":"A"},{"questionId":"2","answer":"B"}]

Wrapper class SurveyAnswerPaylod is needed here to get it wrapped in a parent "{"answers":[{"questionId":"1","answer":"A"},{"questionId":"2","answer":"B"}]}"

class SurveyAnswerPayload{
  @Json(name = "answers")
  public List<SurveyAnswer> answers;
}
@serj-lotutovici
Copy link
Owner

Check out LazyAdaptersRetrofitTest.java there's a service method that utilizes this.
Change SurveyService to be:

public interface SurveyService{
  @POST("surveys/{surveyId}/answers")
  Call<Void> postAnswers(@Path("surveyId") String surveyId,   @Wrapped(path = {"answers"}) @Body List<SurveyAnswer> answers);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants