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, unknown map to binary data #4738

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
2 changes: 2 additions & 0 deletions packages/http-client-java/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface EmitterOptions {

"enable-sync-stack"?: boolean;
"stream-style-serialization"?: boolean;
"use-object-for-unknown"?: boolean;

"partial-update"?: boolean;
"models-subpackage"?: string;
Expand Down Expand Up @@ -77,6 +78,7 @@ const EmitterOptionsSchema: JSONSchemaType<EmitterOptions> = {

"enable-sync-stack": { type: "boolean", nullable: true, default: true },
"stream-style-serialization": { type: "boolean", nullable: true, default: true },
"use-object-for-unknown": { type: "boolean", nullable: true, default: true },

// customization
"partial-update": { type: "boolean", nullable: true, default: false },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public static JavaSettings getInstance() {
getBooleanValue(host, "null-byte-array-maps-to-empty-array", false),
getBooleanValue(host, "graal-vm-config", false), getStringValue(host, "flavor", "Azure"),
getBooleanValue(host, "disable-typed-headers-methods", false),
getBooleanValue(host, "share-jsonserializable-code", false), getBooleanValue(host, "android", false));
getBooleanValue(host, "share-jsonserializable-code", false),
getBooleanValue(host, "use-object-for-unknown", false), getBooleanValue(host, "android", false));
}
return instance;
}
Expand Down Expand Up @@ -293,7 +294,8 @@ private JavaSettings(AutorestSettings autorestSettings, Map<String, Object> mode
boolean includeReadOnlyInConstructorArgs, boolean urlAsString, boolean uuidAsString,
boolean disableRequiredPropertyAnnotation, boolean pageSizeEnabled, boolean useKeyCredential,
boolean nullByteArrayMapsToEmptyArray, boolean generateGraalVmConfig, String flavor,
boolean disableTypedHeadersMethods, boolean shareJsonSerializableCode, boolean android) {
boolean disableTypedHeadersMethods, boolean shareJsonSerializableCode, boolean useObjectForUnknown,
boolean android) {

this.autorestSettings = autorestSettings;
this.modelerSettings = new ModelerSettings(modelerSettings);
Expand Down Expand Up @@ -392,6 +394,7 @@ private JavaSettings(AutorestSettings autorestSettings, Map<String, Object> mode
this.flavor = flavor;
this.disableTypedHeadersMethods = disableTypedHeadersMethods;
this.shareJsonSerializableCode = shareJsonSerializableCode;
this.useObjectForUnknown = useObjectForUnknown;
this.android = android;
}

Expand Down Expand Up @@ -1607,6 +1610,12 @@ public boolean isShareJsonSerializableCode() {
return shareJsonSerializableCode;
}

private final boolean useObjectForUnknown;

public boolean isUseObjectForUnknown() {
return useObjectForUnknown;
}

private final boolean android;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.microsoft.typespec.http.client.generator.core.mapper;

import com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.AnySchema;
import com.microsoft.typespec.http.client.generator.core.extension.plugin.JavaSettings;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;

Expand All @@ -14,8 +15,7 @@ public class AnyMapper implements IMapper<AnySchema, IType> {

private static final AnyMapper INSTANCE = new AnyMapper();

private AnyMapper() {
// private constructor
protected AnyMapper() {
}

/**
Expand All @@ -29,6 +29,10 @@ public static AnyMapper getInstance() {

@Override
public IType map(AnySchema anySchema) {
return ClassType.OBJECT;
if (JavaSettings.getInstance().isUseObjectForUnknown()) {
return ClassType.OBJECT;
} else {
return ClassType.BINARY_DATA;
}
Comment on lines -32 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the main code change.

}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No logic change in this file. Just being a bit more restricted on imports Jackson (only do this when not in sss).

Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,17 @@ public boolean isRequiredForCreate() {
*/
public final void addImportsTo(Set<String> imports, boolean shouldGenerateXmlSerialization) {
JavaSettings settings = JavaSettings.getInstance();
final boolean requireJackson = !settings.isStreamStyleSerialization();

if (getHeaderCollectionPrefix() != null && !getHeaderCollectionPrefix().isEmpty()) {
Annotation.HEADER_COLLECTION.addImportsTo(imports);
}
if (isAdditionalProperties) {
imports.add("com.fasterxml.jackson.annotation.JsonIgnore");
imports.add("com.fasterxml.jackson.annotation.JsonAnySetter");
imports.add("com.fasterxml.jackson.annotation.JsonAnyGetter");
if (requireJackson) {
imports.add("com.fasterxml.jackson.annotation.JsonIgnore");
imports.add("com.fasterxml.jackson.annotation.JsonAnySetter");
imports.add("com.fasterxml.jackson.annotation.JsonAnyGetter");
}
imports.add(LinkedHashMap.class.getName());
}

Expand All @@ -305,7 +308,7 @@ public final void addImportsTo(Set<String> imports, boolean shouldGenerateXmlSer
addJsonFlattenAnnotationImport(imports);
}

if (!isAdditionalProperties && getClientType() instanceof MapType) {
if (requireJackson && !isAdditionalProperties && getClientType() instanceof MapType) {
// required for "@JsonInclude(value = JsonInclude.Include.NON_NULL, content = JsonInclude.Include.ALWAYS)"
imports.add("com.fasterxml.jackson.annotation.JsonInclude");
}
Expand All @@ -319,16 +322,18 @@ public final void addImportsTo(Set<String> imports, boolean shouldGenerateXmlSer
imports.add(ClassType.CORE_UTILS.getFullName());
}

if (shouldGenerateXmlSerialization) {
imports.add("com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement");
if (isXmlWrapper()) {
imports.add("com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty");
}
if (isXmlText()) {
imports.add("com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText");
if (requireJackson) {
if (shouldGenerateXmlSerialization) {
imports.add("com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement");
if (isXmlWrapper()) {
imports.add("com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty");
}
if (isXmlText()) {
imports.add("com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText");
}
} else {
imports.add("com.fasterxml.jackson.annotation.JsonProperty");
}
} else {
imports.add("com.fasterxml.jackson.annotation.JsonProperty");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,13 @@ private void addImports(Set<String> imports, ClientModel model, JavaSettings set
String lastParentName = model.getName();
ClientModel parentModel = ClientModelUtil.getClientModel(model.getParentModelName());
while (parentModel != null && !lastParentName.equals(parentModel.getName())) {
// implementation code of stream-style serialization refs to the element type of the Map
for (ClientModelProperty parentProperty : parentModel.getProperties()) {
if (parentProperty.isAdditionalProperties()) {
parentProperty.addImportsTo(imports, false);
}
}

imports.addAll(parentModel.getImports());
lastParentName = parentModel.getName();
parentModel = ClientModelUtil.getClientModel(parentModel.getParentModelName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ $generateScript = {
$tspOptions += " --option ""@typespec/http-client-java.api-version=2022-09-01"""
# exclude preview from service versions
$tspOptions += " --option ""@typespec/http-client-java.service-version-exclude-preview=true"""
} elseif ($tspFile -match "type[\\/]array" -or $tspFile -match "type[\\/]dictionary") {
# TODO https://github.com/Azure/autorest.java/issues/2964
haolingdong-msft marked this conversation as resolved.
Show resolved Hide resolved
# also serve as a test for "use-object-for-unknown" emitter option
$tspOptions += " --option ""@typespec/http-client-java.use-object-for-unknown=true"""
} elseif ($tspFile -match "arm.tsp") {
# for mgmt, do not generate tests due to random mock values
$tspOptions += " --option ""@typespec/http-client-java.generate-tests=false"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"testserver-stop": "npx cadl-ranch server stop"
},
"dependencies": {
"@azure-tools/cadl-ranch-specs": "0.37.6",
"@azure-tools/cadl-ranch-specs": "0.37.7",
"@typespec/http-client-java": "file:/../../typespec-http-client-java-0.1.0.tgz",
"@typespec/http-client-java-tests": "file:"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class ExtendsUnknownAsyncClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -75,7 +75,7 @@ public Mono<Response<BinaryData>> getWithResponse(RequestOptions requestOptions)
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class ExtendsUnknownClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -73,7 +73,7 @@ public Response<BinaryData> getWithResponse(RequestOptions requestOptions) {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class ExtendsUnknownDerivedAsyncClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down Expand Up @@ -77,7 +77,7 @@ public Mono<Response<BinaryData>> getWithResponse(RequestOptions requestOptions)
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class ExtendsUnknownDerivedClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down Expand Up @@ -75,7 +75,7 @@ public Response<BinaryData> getWithResponse(RequestOptions requestOptions) {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class ExtendsUnknownDiscriminatedAsyncClient {
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -77,7 +77,7 @@ public Mono<Response<BinaryData>> getWithResponse(RequestOptions requestOptions)
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class ExtendsUnknownDiscriminatedClient {
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -75,7 +75,7 @@ public Response<BinaryData> getWithResponse(RequestOptions requestOptions) {
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class IsUnknownAsyncClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -75,7 +75,7 @@ public Mono<Response<BinaryData>> getWithResponse(RequestOptions requestOptions)
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class IsUnknownClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -73,7 +73,7 @@ public Response<BinaryData> getWithResponse(RequestOptions requestOptions) {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class IsUnknownDerivedAsyncClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down Expand Up @@ -77,7 +77,7 @@ public Mono<Response<BinaryData>> getWithResponse(RequestOptions requestOptions)
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class IsUnknownDerivedClient {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down Expand Up @@ -75,7 +75,7 @@ public Response<BinaryData> getWithResponse(RequestOptions requestOptions) {
* {
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* index: int (Required)
* age: Double (Optional)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class IsUnknownDiscriminatedAsyncClient {
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -77,7 +77,7 @@ public Mono<Response<BinaryData>> getWithResponse(RequestOptions requestOptions)
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class IsUnknownDiscriminatedClient {
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down Expand Up @@ -75,7 +75,7 @@ public Response<BinaryData> getWithResponse(RequestOptions requestOptions) {
* kind: String (Required)
* name: String (Required)
* (Optional): {
* String: Object (Required)
* String: BinaryData (Required)
* }
* }
* }
Expand Down
Loading
Loading