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

Fix JsonSyntaxException: Superfluous data after top-level array in streaming mode #10000

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2017-2019 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.http.client

import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.core.annotation.Introspected
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import reactor.core.publisher.Flux
import spock.lang.Specification

@Property(name = 'spec.name', value = 'JsonSpec')
@MicronautTest
class JsonSpec extends Specification {

@Inject
JsonClient myClient

void readJson() {
when:
Flux<SomeDto> fluxResponse = myClient.getSome()

then:
List<SomeDto> someDtos = fluxResponse.collectList().block()
someDtos.size() == 4
someDtos[0].data == "111"
someDtos[1].data == "222"
someDtos[2].data == "333"
someDtos[3].data == "444"
}

@Requires(property = 'spec.name', value = 'JsonSpec')
@Client('/data')
static interface JsonClient {

@Get
Flux<SomeDto> getSome();

}

@Requires(property = 'spec.name', value = 'JsonSpec')
@Controller('/data')
static class JsonController {

@Get(produces = MediaType.APPLICATION_JSON)
String data() {
return """
[{
"data": "111"
}, {
"data": "222"
}, {
"data": "333"
}, {
"data": "444"
}]
"""
}
}

@Introspected
static class SomeDto {
private String data

String getData() {
return data
}

void setData(String data) {
this.data = data
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private void proceedUntilBuffering(ByteBuf buf) throws JsonSyntaxException {

if (state == State.AFTER_UNWRAP_ARRAY) {
// top-level array consumed. reject further data
skipWs(buf, i, end);
i = skipWs(buf, i, end);
if (i < end) {
throw new JsonSyntaxException("Superfluous data after top-level array in streaming mode");
}
Expand Down