Skip to content

Commit d6cbe8d

Browse files
http-client-java, ignore error on invalid date time value in JSON example (#10262)
appears in service Azure/azure-rest-api-specs#42087
1 parent 83b5bf3 commit d6cbe8d

3 files changed

Lines changed: 42 additions & 18 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-client-java"
5+
---
6+
7+
Ignore error in JSON example, if the format is incorrect.

packages/http-client-java/emitter/src/emitter.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,23 @@ function reportJarOutput(program: Program, jarOutput: string) {
185185
}
186186

187187
// trace or report the logs, according to log level
188+
const typeSpecPluginPrefix = "com.microsoft.typespec.http.client.generator.";
189+
const errorPrefix = "ERROR " + typeSpecPluginPrefix;
190+
const warnPrefix = "WARN " + typeSpecPluginPrefix;
188191
for (const log of logs) {
189-
if (log.startsWith("ERROR ")) {
192+
if (log.startsWith(errorPrefix)) {
190193
reportDiagnostic(program, {
191194
code: "generator-error",
192195
format: {
193-
errorMessage: log.substring(6),
196+
errorMessage: log.substring(errorPrefix.length),
194197
},
195198
target: NoTarget,
196199
});
197-
} else if (log.startsWith("WARN ")) {
200+
} else if (log.startsWith(warnPrefix)) {
198201
reportDiagnostic(program, {
199202
code: "generator-warning",
200203
format: {
201-
warningMessage: log.substring(5),
204+
warningMessage: log.substring(warnPrefix.length),
202205
},
203206
target: NoTarget,
204207
});

packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/util/WireTypeClientTypeConverter.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3+
34
package com.microsoft.typespec.http.client.generator.core.util;
45

6+
import com.microsoft.typespec.http.client.generator.core.Javagen;
7+
import com.microsoft.typespec.http.client.generator.core.extension.plugin.PluginLogger;
58
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ClassType;
69
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.IType;
710
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.PrimitiveType;
@@ -10,12 +13,16 @@
1013
import java.time.Duration;
1114
import java.time.Instant;
1215
import java.time.ZoneOffset;
16+
import org.slf4j.Logger;
1317

1418
/**
1519
* Class to group conversion logic between client type and wire type.
1620
*/
1721
public class WireTypeClientTypeConverter {
1822

23+
private final static Logger LOGGER
24+
= new PluginLogger(Javagen.getPluginInstance(), WireTypeClientTypeConverter.class);
25+
1926
private WireTypeClientTypeConverter() {
2027
}
2128

@@ -158,20 +165,27 @@ public static String convertToWireTypeExpression(PrimitiveType clientType, Strin
158165
*/
159166
public static String convertLiteralToClientValue(IType wireType, String literalInWireType) {
160167
String literalValue = literalInWireType;
161-
if (wireType == ClassType.DATE_TIME_RFC_1123) {
162-
literalValue = new DateTimeRfc1123(literalValue).getDateTime().toString();
163-
} else if (wireType == ClassType.BASE_64_URL) {
164-
literalValue = new Base64Uri(literalValue).toString();
165-
} else if (wireType.asNullable() == ClassType.UNIX_TIME_LONG) {
166-
literalValue = Instant.ofEpochSecond(Long.parseLong(literalValue)).atOffset(ZoneOffset.UTC).toString();
167-
} else if (wireType.asNullable() == ClassType.DURATION_LONG) {
168-
literalValue = Duration.ofSeconds(Long.parseLong(literalValue)).toString();
169-
} else if (wireType.asNullable() == ClassType.DURATION_DOUBLE) {
170-
literalValue = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000_000L)).toString();
171-
} else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_LONG) {
172-
literalValue = Duration.ofMillis(Long.parseLong(literalValue)).toString();
173-
} else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_DOUBLE) {
174-
literalValue = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000L)).toString();
168+
try {
169+
if (wireType == ClassType.DATE_TIME_RFC_1123) {
170+
literalValue = new DateTimeRfc1123(literalValue).getDateTime().toString();
171+
} else if (wireType == ClassType.BASE_64_URL) {
172+
literalValue = new Base64Uri(literalValue).toString();
173+
} else if (wireType.asNullable() == ClassType.UNIX_TIME_LONG) {
174+
literalValue = Instant.ofEpochSecond(Long.parseLong(literalValue)).atOffset(ZoneOffset.UTC).toString();
175+
} else if (wireType.asNullable() == ClassType.DURATION_LONG) {
176+
literalValue = Duration.ofSeconds(Long.parseLong(literalValue)).toString();
177+
} else if (wireType.asNullable() == ClassType.DURATION_DOUBLE) {
178+
literalValue
179+
= Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000_000L)).toString();
180+
} else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_LONG) {
181+
literalValue = Duration.ofMillis(Long.parseLong(literalValue)).toString();
182+
} else if (wireType.asNullable() == ClassType.DURATION_MILLISECONDS_DOUBLE) {
183+
literalValue = Duration.ofNanos((long) (Double.parseDouble(literalInWireType) * 1000_000L)).toString();
184+
}
185+
} catch (RuntimeException e) {
186+
LOGGER.warn(
187+
"Failed to convert literal value '{}' from wire type to client type. Return the original literal value. Error: {}",
188+
literalInWireType, e.getMessage());
175189
}
176190
return literalValue;
177191
}

0 commit comments

Comments
 (0)