-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for encoding numeric types as string (#4020)
fix #3856
- Loading branch information
1 parent
44ffaf7
commit 44fc030
Showing
14 changed files
with
313 additions
and
171 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
.chronus/changes/encode-numeric-as-string-2024-6-25-20-9-18.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
changeKind: feature | ||
packages: | ||
- "@typespec/compiler" | ||
- "@typespec/openapi3" | ||
- "@typespec/xml" | ||
--- | ||
|
||
Add support for encoding numeric types as string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { type ModelProperty, Program, type Scalar, getEncode } from "@typespec/compiler"; | ||
import type { ResolvedOpenAPI3EmitterOptions } from "./openapi.js"; | ||
import { getSchemaForStdScalars } from "./std-scalar-schemas.js"; | ||
import type { OpenAPI3Schema } from "./types.js"; | ||
|
||
export function applyEncoding( | ||
program: Program, | ||
typespecType: Scalar | ModelProperty, | ||
target: OpenAPI3Schema, | ||
options: ResolvedOpenAPI3EmitterOptions | ||
): OpenAPI3Schema { | ||
const encodeData = getEncode(program, typespecType); | ||
if (encodeData) { | ||
const newTarget = { ...target }; | ||
const newType = getSchemaForStdScalars(encodeData.type as any, options); | ||
newTarget.type = newType.type; | ||
// If the target already has a format it takes priority. (e.g. int32) | ||
newTarget.format = mergeFormatAndEncoding( | ||
newTarget.format, | ||
encodeData.encoding, | ||
newType.format | ||
); | ||
return newTarget; | ||
} | ||
return target; | ||
} | ||
|
||
function mergeFormatAndEncoding( | ||
format: string | undefined, | ||
encoding: string | undefined, | ||
encodeAsFormat: string | undefined | ||
): string | undefined { | ||
switch (format) { | ||
case undefined: | ||
return encodeAsFormat ?? encoding ?? format; | ||
case "date-time": | ||
switch (encoding) { | ||
case "rfc3339": | ||
return "date-time"; | ||
case "unixTimestamp": | ||
return "unixtime"; | ||
case "rfc7231": | ||
return "http-date"; | ||
default: | ||
return encoding; | ||
} | ||
case "duration": | ||
switch (encoding) { | ||
case "ISO8601": | ||
return "duration"; | ||
default: | ||
return encodeAsFormat ?? encoding; | ||
} | ||
default: | ||
return encodeAsFormat ?? encoding ?? format; | ||
} | ||
} |
Oops, something went wrong.