googleapis / google-http-java-client

Google HTTP Client Library for Java
Apache License 2.0
1.39k stars 448 forks source link

Sheets V4 append with sheet name including "+" broken since google-http-client version 1.34.0 #1573

Open mihalyr opened 2 years ago

mihalyr commented 2 years ago

As reported at https://issuetracker.google.com/issues/217399871

The sheets.spreadsheets().values().append operation is failing if the sheet name includes a plus ("+") sign and has a dependency on google-http-client version 1.34.0 or older.

The breaking change is likely the result of https://github.com/googleapis/google-http-java-client/pull/913

Library versions:

    implementation 'com.google.apis:google-api-services-drive:v2-rev212-1.21.0'
    implementation 'com.google.apis:google-api-services-sheets:v4-rev612-1.25.0'
    testImplementation 'com.google.api-client:google-api-client:1.25.0'
    testImplementation 'com.google.http-client:google-http-client:1.34.0'
    testImplementation 'com.google.http-client:google-http-client-jackson2:1.34.0'

Reproducer:

    @Test
    void testAppendWithPlusSignSheet() throws Exception {
        String fileName = "test-append-plus-sign";
        String sheetName = "plus+sign";
        String spreadsheetId = spreadsheetHelper.createSpreadsheet(fileName, sheetName);

        String range = sheetName + "!A1";
        ValueRange valueRange = new ValueRange();
        valueRange.setMajorDimension(SheetOptions.MAJOR_DIMENSION_ROWS);
        valueRange.setRange(range);
        valueRange.setValues(ImmutableList.of(ImmutableList.of("test")));

        try {
            Sheets sheets = authHelper.getSheetsService();

            sheets.spreadsheets().values()
                    .append(spreadsheetId, range, valueRange)
                    .setValueInputOption(SheetOptions.VALUE_INPUT_RAW)
                    .setInsertDataOption(SheetOptions.INSERT_DATA_ROWS)
                    .setIncludeValuesInResponse(true)
                    .setResponseValueRenderOption(SheetOptions.VALUE_RENDERING_UNFORMATTED)
                    .setResponseDateTimeRenderOption(SheetOptions.DATE_RENDERING_SERIAL)
                    .execute();
        } finally {
            Drive drive = authHelper.getDriveService();
            FileList fileList = drive.files().list().execute();
            for (File file : fileList.getItems()) {
                if (file.getTitle().equals(sheetTitle)) {
                    drive.files().delete(file.getId()).execute();
                }
            }
        }
    }

This was working fine up to google-http-client version 1.33.0, when used with a newer version it fails with the following exception:

    com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
    {
      "code" : 400,
      "errors" : [ {
        "domain" : "global",
        "message" : "Request range[plus sign!A1] does not match value's range[plus+sign!A1]",
        "reason" : "badRequest"
      } ],
      "message" : "Request range[plus sign!A1] does not match value's range[plus+sign!A1]",
      "status" : "INVALID_ARGUMENT"
    }
mihalyr commented 6 months ago

Hey there, just chiming in that today we updated to the latest Sheets V4 java package that uses com.google.http-client:google-http-client:1.42.3 and we are still facing this issue with the plus sign. The only workaround for us is to add the sign to some unallowed character list and prevent our users creating sheets with such names. This is a bit awkward as there might be users already with plus signs and this defect will force them to make changes to their sheets.