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
18 changes: 18 additions & 0 deletions sdks/typescript/scripts/normalize_generated_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,30 @@ def _normalize_sdk_metadata_version(file_path: Path) -> None:
file_path.write_text(normalized, encoding="utf-8")


def _strip_unstable_type_comments(file_path: Path) -> None:
original = file_path.read_text(encoding="utf-8")
normalized = original.replace(
"/**\n"
" * Template-backed input payload for control create/update requests.\n"
" */\n"
"export type TemplateControlInput = {",
"export type TemplateControlInput = {",
)

if normalized != original:
file_path.write_text(normalized, encoding="utf-8")


def normalize_generated_sdk(_schema_path: Path, generated_dir: Path) -> None:
"""Normalize generated SDK output to keep generation deterministic."""
config_file = generated_dir / "lib" / "config.ts"
if config_file.exists():
_normalize_sdk_metadata_version(config_file)

template_control_input_file = generated_dir / "models" / "template-control-input.ts"
if template_control_input_file.exists():
_strip_unstable_type_comments(template_control_input_file)

validation_error_file = generated_dir / "models" / "validation-error.ts"
if not validation_error_file.exists():
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import {
TemplateValue$outboundSchema,
} from "./template-value.js";

/**
* Template-backed input payload for control create/update requests.
*/
export type TemplateControlInput = {
/**
* Reusable template with typed parameters and a JSON definition template.
Expand Down
50 changes: 50 additions & 0 deletions sdks/typescript/tests/test_normalize_generated_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@
"""


TEMPLATE_CONTROL_INPUT_WITHOUT_UNSTABLE_TYPE_COMMENT = """/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/

export type TemplateControlInput = {};
"""


TEMPLATE_CONTROL_INPUT_WITH_UNSTABLE_TYPE_COMMENT = """/*
* Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
*/

/**
* Template-backed input payload for control create/update requests.
*/
export type TemplateControlInput = {};
"""


def _write_schema(tmp_path: Path, *, include_context_fields: bool) -> Path:
properties: dict[str, object] = {
"loc": {"type": "array"},
Expand Down Expand Up @@ -140,6 +159,10 @@ def _write_generated_file(tmp_path: Path) -> Path:
lib_dir.mkdir(parents=True)
file_path = models_dir / "validation-error.ts"
file_path.write_text(VALIDATION_ERROR_WITH_CONTEXT, encoding="utf-8")
(models_dir / "template-control-input.ts").write_text(
TEMPLATE_CONTROL_INPUT_WITHOUT_UNSTABLE_TYPE_COMMENT,
encoding="utf-8",
)
(lib_dir / "config.ts").write_text(CONFIG_WITH_VERSIONED_METADATA, encoding="utf-8")
return generated_dir

Expand Down Expand Up @@ -212,3 +235,30 @@ def test_normalizer_rewrites_sdk_metadata_to_stable_version(tmp_path: Path) -> N
'userAgent: "speakeasy-sdk/typescript 0.1.0 2.827.0 0.0.0 agent-control"'
in normalized
)


def test_normalizer_removes_unstable_template_control_input_type_comment(
tmp_path: Path,
) -> None:
schema_path = _write_schema(tmp_path, include_context_fields=True)
generated_dir = _write_generated_file(tmp_path)
template_control_input = generated_dir / "models" / "template-control-input.ts"
template_control_input.write_text(
TEMPLATE_CONTROL_INPUT_WITH_UNSTABLE_TYPE_COMMENT,
encoding="utf-8",
)

subprocess.run(
[
sys.executable,
str(SCRIPT_PATH),
"--schema",
str(schema_path),
"--generated-dir",
str(generated_dir),
],
check=True,
)

normalized = template_control_input.read_text(encoding="utf-8")
assert normalized == TEMPLATE_CONTROL_INPUT_WITHOUT_UNSTABLE_TYPE_COMMENT
Loading