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

http-client-java, support (non-nested) continuationToken for unbranded #6143

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

weidongxu-microsoft
Copy link
Contributor

@weidongxu-microsoft weidongxu-microsoft commented Feb 25, 2025

link #5792

continuationToken in nested (not top-level) property of response is currently not supported in core, hence this PR didn't include this special case.

API (I've added a body to demonstrate the case that 2nd call would get all input from 1st -- query and header would already be absorbed in RequestOptions)

    @Metadata(generated = true)
    @ServiceMethod(returns = ReturnType.COLLECTION)
    public PagedIterable<Pet> requestHeaderResponseHeader(Body body) {
        // Generated convenience method for requestHeaderResponseHeader
        RequestOptions requestOptions = new RequestOptions();
        return serviceClient.requestHeaderResponseHeader(BinaryData.fromObject(body), requestOptions);
    }

Impl

    private PagedResponse<Pet> requestHeaderResponseHeaderSinglePage(BinaryData body, RequestOptions requestOptions) {
        final String accept = "application/json";
        Response<RequestHeaderResponseHeaderResponse> res
            = service.requestHeaderResponseHeaderSync(this.getEndpoint(), accept, body, requestOptions);
        return new PagedResponse<>(res.getRequest(), res.getStatusCode(), res.getHeaders(), res.getBody(),
            res.getValue().getPets(), res.getHeaders().getValue(HttpHeaderName.fromString("next-token")), null, null,
            null, null);
    }

    public PagedIterable<Pet> requestHeaderResponseHeader(BinaryData body, RequestOptions requestOptions) {
        return new PagedIterable<>(pagingOptions -> {
            if (pagingOptions.getOffset() != null) {
                throw LOGGER.logThrowableAsError(new IllegalArgumentException(
                    "'offset' in PagingOptions is not supported in API 'requestHeaderResponseHeader'."));
            }
            if (pagingOptions.getPageSize() != null) {
                throw LOGGER.logThrowableAsError(new IllegalArgumentException(
                    "'pageSize' in PagingOptions is not supported in API 'requestHeaderResponseHeader'."));
            }
            if (pagingOptions.getPageIndex() != null) {
                throw LOGGER.logThrowableAsError(new IllegalArgumentException(
                    "'pageIndex' in PagingOptions is not supported in API 'requestHeaderResponseHeader'."));
            }
            RequestOptions requestOptionsLocal = requestOptions == null ? new RequestOptions() : requestOptions;
            if (pagingOptions.getContinuationToken() != null) {
                requestOptionsLocal.setHeader(HttpHeaderName.fromString("token"),
                    String.valueOf(pagingOptions.getContinuationToken()));
            }
            return requestHeaderResponseHeaderSinglePage(body, requestOptionsLocal);
        });
    }

user code

pagedIterable = tokenClient.requestQueryResponseHeader();

pagedIterable.stream()...

pagedIterable.streamByPage(new PagingOptions().setContinuationToken("page2"))...

// throws on pagedIterable.streamByPage(new PagingOptions().setPageSize(10L))...
// "offset" and "pageIndex" is not handled yet, and need related code in clientcore

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:java Issue for the Java client emitter: @typespec/http-client-java label Feb 25, 2025
@azure-sdk
Copy link
Collaborator

azure-sdk commented Feb 25, 2025

❌ There is undocummented changes. Run chronus add to add a changeset or click here.

The following packages have changes but are not documented.

  • @typespec/http-client-java
Show changes

@azure-sdk
Copy link
Collaborator

azure-sdk commented Feb 25, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

Copy link
Contributor Author

@weidongxu-microsoft weidongxu-microsoft Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added inner class ContinuationToken

refactor here, to use either ModelPropertySegment or List<ModelPropertySegment> for the reference of property (instead of e.g. just String itemName)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impl code of generated SDK is mainly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@weidongxu-microsoft weidongxu-microsoft Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code under http-client-generator-test can be ignored. We didn't support continuationToken in branded, yet.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 28 out of 43 changed files in this pull request and generated no comments.

Files not reviewed (15)
  • packages/http-client-java/generator/http-client-generator-clientcore-test/package.json: Language not supported
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/resources/META-INF/payload-pageable_apiview_properties.json: Language not supported
  • packages/http-client-java/generator/http-client-generator-core/pom.xml: Language not supported
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/PageableClientImpl.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/ServerDrivenPaginationClient.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/extension/model/extensionmodel/XmsPageable.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/PageableClientBuilder.java: Evaluated as low risk
  • packages/http-client-java/emitter/src/code-model-builder.ts: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/model/clientmodel/ClientMethod.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ClientMethodMapper.java: Evaluated as low risk
  • packages/http-client-java/emitter/src/common/client.ts: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/ServerDrivenPaginationsImpl.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/test/java/payload/pageable/PageableTests.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/serverdrivenpagination/continuationtoken/implementation/package-info.java: Evaluated as low risk
  • packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/ServerDrivenPaginationContinuationTokenClient.java: Evaluated as low risk
Comments suppressed due to low confidence (2)

packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/RequestQueryResponseHeaderResponse.java:54

  • The toJson method should call writeEndObject after writing the array field to properly close the JSON object.
return jsonWriter.writeEndObject();

packages/http-client-java/generator/http-client-generator-clientcore-test/src/main/java/payload/pageable/implementation/RequestHeaderResponseHeaderResponse.java:55

  • The toJson method should return the JsonWriter object after writing the end object. Update the method to return jsonWriter.writeEndObject().
return jsonWriter.writeEndObject();
Copy link
Member

@anuchandy anuchandy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty! Weidong..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:java Issue for the Java client emitter: @typespec/http-client-java
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants