Skip to content
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,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http-client-java"
---

Use `LinkedHashMap` and `LinkedHashSet` to ensure consistent iterating order.
5 changes: 2 additions & 3 deletions packages/http-client-java/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Changelog - @typespec/http-client-java



## 0.7.0

### Features
Expand All @@ -22,4 +20,5 @@
- [#9677](https://github.com/microsoft/typespec/pull/9677) Fix incorrect variable name of continuationToken
- [#9527](https://github.com/microsoft/typespec/pull/9527) Missing example value for BinaryData type in mock test.
- [#9639](https://github.com/microsoft/typespec/pull/9639) Fix mock data for BinaryData.

- [#9751](https://github.com/microsoft/typespec/pull/9751) Use `LinkedHashMap` and `LinkedHashSet` to ensure consistent
iterating order.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.microsoft.typespec.http.client.generator.core.util.ClientModelUtil;
import com.microsoft.typespec.http.client.generator.core.util.SchemaUtil;
import io.clientcore.core.utils.CoreUtils;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -166,7 +165,7 @@ protected JavaPackage writeToTemplates(CodeModel codeModel, Client client, JavaS
&& client.getSyncClients().stream().anyMatch(c -> c.getClientBuilder() != null)) {
List<ServiceClient> serviceClients = client.getServiceClients();
if (CoreUtils.isNullOrEmpty(serviceClients)) {
serviceClients = Collections.singletonList(client.getServiceClient());
serviceClients = List.of(client.getServiceClient());
}
TestContext<Void> testContext = new TestContext<>(serviceClients, client.getSyncClients());

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -18,7 +17,7 @@ public class Client extends Metadata {
private List<ApiVersion> apiVersions = new ArrayList<>();
private ServiceVersion serviceVersion;
private Client parent;
private List<Client> subClients = Collections.emptyList();
private List<Client> subClients = List.of();
private boolean buildMethodPublic = true;
private boolean parentAccessorPublic = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.microsoft.typespec.http.client.generator.core.extension.model.extensionmodel.XmsExamples;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.yaml.snakeyaml.LoaderOptions;
Expand Down Expand Up @@ -381,7 +381,7 @@ public Object construct(Node node) {
protected Object constructJavaBean2ndStep(MappingNode node, Object object) {
if (node.getType().equals(XmsExamples.class)) {
// deserialize to Map<String, Object>, while Object would be LinkedHashMap
Map<String, Object> examples = new HashMap<>();
Map<String, Object> examples = new LinkedHashMap<>();
for (NodeTuple tuple : node.getValue()) {
examples.put(((ScalarNode) tuple.getKeyNode()).getValue(), constructObject(tuple.getValueNode()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class OperationGroup extends Metadata {
private String $key;
private List<Operation> operations = new ArrayList<Operation>();
private List<Operation> operations = new ArrayList<>();
private Client codeModel;

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

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

Expand All @@ -14,7 +14,7 @@
public class Scheme {
private Scheme.SecuritySchemeType type;
// OAuth2
private Set<String> scopes = new HashSet<>();
private Set<String> scopes = new LinkedHashSet<>();
private List<OAuth2Flow> flows = new ArrayList<>();
// Key
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

package com.microsoft.typespec.http.client.generator.core.extension.model.extensionmodel;

import java.util.HashMap;
import java.util.Map;

/**
* Represents the HTTP verb for the nextLink operation in pageable settings.
*/
Expand All @@ -21,13 +18,6 @@ public enum NextLinkVerb {
POST("POST");

private final String value;
private static final Map<String, NextLinkVerb> CONSTANTS = new HashMap<>();

static {
for (NextLinkVerb v : values()) {
CONSTANTS.put(v.value, v);
}
}

NextLinkVerb(String value) {
this.value = value;
Expand Down Expand Up @@ -55,11 +45,11 @@ public String value() {
* @throws IllegalArgumentException If the value is not a valid HTTP verb.
*/
public static NextLinkVerb fromValue(String value) {
NextLinkVerb constant = CONSTANTS.get(value);
if (constant == null) {
throw new IllegalArgumentException(value);
} else {
return constant;
if (GET.value.equals(value)) {
return GET;
} else if (POST.value.equals(value)) {
return POST;
}
throw new IllegalArgumentException(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -34,7 +34,7 @@ public class JavaSettings {
private static JavaSettings instance;
private static NewPlugin host;
private static String header;
private static final Map<String, Object> SIMPLE_JAVA_SETTINGS = new HashMap<>();
private static final Map<String, Object> SIMPLE_JAVA_SETTINGS = new LinkedHashMap<>();
private static Logger logger;
private final boolean useKeyCredential;
private final String flavor;
Expand Down Expand Up @@ -70,7 +70,7 @@ static void setHeader(String value) {
}
}

static void setHost(NewPlugin host) {
public static void setHost(NewPlugin host) {
JavaSettings.host = host;
logger = new PluginLogger(host, JavaSettings.class);
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public static JavaSettings getInstance() {

private static Map<Integer, String> parseStatusCodeMapping(JsonReader jsonReader) throws IOException {
return jsonReader.readObject(reader -> {
Map<Integer, String> mapping = new HashMap<>();
Map<Integer, String> mapping = new LinkedHashMap<>();
while (reader.nextToken() != JsonToken.END_OBJECT) {
int key = Integer.parseInt(reader.getFieldName());
reader.nextToken();
Expand Down Expand Up @@ -649,7 +649,7 @@ public static class ModelerSettings {
* @param settings The settings that are used by the modeler.
*/
public ModelerSettings(Map<String, Object> settings) {
this.settings = settings == null ? Collections.emptyMap() : settings;
this.settings = settings == null ? Map.of() : settings;
}

/**
Expand Down Expand Up @@ -1523,7 +1523,7 @@ public boolean isUseObjectForUnknown() {
return useObjectForUnknown;
}

private final Map<String, String> renameModel = new HashMap<>();
private final Map<String, String> renameModel = new LinkedHashMap<>();

public Map<String, String> getJavaNamesForRenameModel() {
return renameModel;
Expand Down Expand Up @@ -1602,7 +1602,7 @@ private static void loadStringOrArraySettingAsArray(String settingName, Consumer
} else {
// Single values will be returned as the string representation.
logger.debug("Option, string, {} : {}", settingName, jsonString);
action.accept(Collections.singletonList(jsonString));
action.accept(List.of(jsonString));
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -192,7 +191,7 @@ public void message(MessageChannel channel, String text, Throwable error, List<S
Message message = new Message();
message.setChannel(channel);
message.setKey(keys);
message.setSource(Collections.emptyList());
message.setSource(List.of());
if (error != null) {
text += "\n" + formatThrowableMessage(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.clientcore.core.utils.CoreUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -291,9 +290,8 @@ public ClientModelPropertiesManager(ClientModel model, JavaSettings settings) {
+ "Add additional possible XmlReader name variables to resolve this issue.");
}

this.xmlNamespaceToConstantMapping = model.getXmlName() == null
? Collections.emptyMap()
: ClientModelUtil.xmlNamespaceToConstantMapping(model);
this.xmlNamespaceToConstantMapping
= model.getXmlName() == null ? Map.of() : ClientModelUtil.xmlNamespaceToConstantMapping(model);
}

private static void superPropertyConsumer(ClientModelProperty property,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@
import io.clientcore.core.utils.CoreUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -90,7 +88,7 @@ public Client map(CodeModel codeModel) {

// enum model
final List<EnumType> enumTypes = new ArrayList<>();
Set<String> enumNames = new HashSet<>();
Set<String> enumNames = new LinkedHashSet<>();
for (ChoiceSchema choiceSchema : codeModel.getSchemas().getChoices()) {
IType iType = Mappers.getChoiceMapper().map(choiceSchema);
if (iType != ClassType.STRING) {
Expand All @@ -117,7 +115,7 @@ public Client map(CodeModel codeModel) {
List<ClientException> exceptions = Stream
.concat(
codeModel.getClients() == null
? Stream.<OperationGroup>empty()
? Stream.empty()
: codeModel.getClients().stream().flatMap(c -> c.getOperationGroups().stream()),
codeModel.getOperationGroups().stream())
.flatMap(og -> og.getOperations().stream())
Expand Down Expand Up @@ -211,7 +209,7 @@ public Client map(CodeModel codeModel) {

// package info
// client
Map<String, PackageInfo> packageInfos = new HashMap<>();
Map<String, PackageInfo> packageInfos = new LinkedHashMap<>();
if (settings.isGenerateClientInterfaces()
|| !settings.isGenerateClientAsImpl()
|| settings.getImplementationSubpackage() == null
Expand Down Expand Up @@ -328,10 +326,9 @@ public Client map(CodeModel codeModel) {
? syncClient.getClassName()
: asyncClient.getClassName().replace("AsyncClient", "Client"));
String clientBuilderName = clientName + builderSuffix;
ClientBuilder clientBuilder
= new ClientBuilder(builderPackage, clientBuilderName, serviceClient,
(syncClient == null) ? Collections.emptyList() : Collections.singletonList(syncClient),
Collections.singletonList(asyncClient), serviceClient.getCrossLanguageDefinitionId());
ClientBuilder clientBuilder = new ClientBuilder(builderPackage, clientBuilderName,
serviceClient, (syncClient == null) ? List.of() : List.of(syncClient), List.of(asyncClient),
serviceClient.getCrossLanguageDefinitionId());

addBuilderTraits(clientBuilder, serviceClient);
clientBuilders.add(clientBuilder);
Expand Down Expand Up @@ -381,7 +378,7 @@ public Client map(CodeModel codeModel) {
private void addConvenienceExamples(Client.Builder builder, List<AsyncSyncClient> syncClients) {
// convenience examples
List<ClientMethodExample> convenienceExamples = new ArrayList<>();
Set<String> convenienceExampleNameSet = new HashSet<>();
Set<String> convenienceExampleNameSet = new LinkedHashSet<>();

BiConsumer<AsyncSyncClient, ConvenienceMethod> handleConvenienceExample = (c, convenienceMethod) -> {
ClientBuilder clientBuilder = c.getClientBuilder();
Expand Down Expand Up @@ -419,7 +416,7 @@ private void addConvenienceExamples(Client.Builder builder, List<AsyncSyncClient

private void addProtocolExamples(Client.Builder builder, List<AsyncSyncClient> syncClients) {
List<ProtocolExample> protocolExamples = new ArrayList<>();
Set<String> protocolExampleNameSet = new HashSet<>();
Set<String> protocolExampleNameSet = new LinkedHashSet<>();

BiConsumer<AsyncSyncClient, ClientMethod> handleExample = (c, m) -> {
if (m.getMethodVisibility() == JavaVisibility.Public
Expand All @@ -445,7 +442,7 @@ private void addProtocolExamples(Client.Builder builder, List<AsyncSyncClient> s

// protocol examples, exclude those that have convenience methods
syncClients.stream().filter(c -> c.getServiceClient() != null).forEach(c -> {
Set<String> convenienceProxyMethodNames = new HashSet<>();
Set<String> convenienceProxyMethodNames = new LinkedHashSet<>();
if (c.getConvenienceMethods() != null) {
convenienceProxyMethodNames.addAll(c.getConvenienceMethods()
.stream()
Expand All @@ -459,7 +456,7 @@ private void addProtocolExamples(Client.Builder builder, List<AsyncSyncClient> s
.forEach(m -> handleExample.accept(c, m));
});
syncClients.stream().filter(c -> c.getMethodGroupClient() != null).forEach(c -> {
Set<String> convenienceProxyMethodNames = new HashSet<>();
Set<String> convenienceProxyMethodNames = new LinkedHashSet<>();
if (c.getConvenienceMethods() != null) {
convenienceProxyMethodNames.addAll(c.getConvenienceMethods()
.stream()
Expand All @@ -484,7 +481,7 @@ private void addProtocolExamples(Client.Builder builder, List<AsyncSyncClient> s
protected Map<ServiceClient, com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Client>
processClients(List<com.microsoft.typespec.http.client.generator.core.extension.model.codemodel.Client> clients,
CodeModel codeModel) {
return Collections.emptyMap();
return Map.of();
}

private void addBuilderTraits(ClientBuilder clientBuilder, ServiceClient serviceClient) {
Expand Down Expand Up @@ -564,9 +561,9 @@ public ObjectSchema parseHeader(Operation operation, JavaSettings settings) {

String name = CodeNamer.getPlural(operation.getOperationGroup().getLanguage().getJava().getName())
+ CodeNamer.toPascalCase(operation.getLanguage().getJava().getName()) + "Headers";
Map<String, Schema> headerMap = new HashMap<>();
Map<String, String> headerClientNameMap = new HashMap<>();
Map<String, XmsExtensions> headerExtensions = new HashMap<>();
Map<String, Schema> headerMap = new LinkedHashMap<>();
Map<String, String> headerClientNameMap = new LinkedHashMap<>();
Map<String, XmsExtensions> headerExtensions = new LinkedHashMap<>();
for (Response response : operation.getResponses()) {
if (response.getProtocol().getHttp().getHeaders() != null) {
for (Header header : response.getProtocol().getHttp().getHeaders()) {
Expand All @@ -585,7 +582,7 @@ public ObjectSchema parseHeader(Operation operation, JavaSettings settings) {
headerSchema.getLanguage().getJava().setName(name);
headerSchema.setProperties(new ArrayList<>());
headerSchema.setStronglyTypedHeader(true);
headerSchema.setUsage(new HashSet<>(Collections.singletonList(SchemaContext.OUTPUT)));
headerSchema.setUsage(new LinkedHashSet<>(List.of(SchemaContext.OUTPUT)));

// TODO (weidxu): at present we do not generate convenience API with Header model
// if (operation.getConvenienceApi() != null) {
Expand Down Expand Up @@ -697,7 +694,7 @@ private static ModuleInfo getModuleInfo(List<String> modelsPackages, Collection<
protected List<String> getModelsPackages(List<ClientModel> clientModels, List<EnumType> enumTypes,
List<ClientResponse> responseModels) {

List<String> ret = Collections.emptyList();
List<String> ret = List.of();

JavaSettings settings = JavaSettings.getInstance();
boolean hasModels = !settings.isDataPlaneClient() // not DPG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ProxyMethodParameter;
import com.microsoft.typespec.http.client.generator.core.util.MethodUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -28,7 +28,7 @@ static ClientMethodParametersDetails process(Request request, boolean mapFluxByt
final List<Parameter> codeModelParameters = getCodeModelParameters(request, isProtocolMethod);
final List<ParametersTuple> parametersTuples = new ArrayList<>();
final List<String> requiredNullableParameterExpressions = new ArrayList<>();
final Map<String, String> validateParameterExpressions = new HashMap<>();
final Map<String, String> validateParameterExpressions = new LinkedHashMap<>();
final boolean isJsonPatch = MethodUtil.isContentTypeInRequest(request, "application/json-patch+json");

final ParametersTransformationProcessor transformationProcessor
Expand Down
Loading
Loading