diff --git a/packages/rlc-common/src/helpers/valueGenerationUtil.ts b/packages/rlc-common/src/helpers/valueGenerationUtil.ts index 9ff28e179f..2d870ae276 100644 --- a/packages/rlc-common/src/helpers/valueGenerationUtil.ts +++ b/packages/rlc-common/src/helpers/valueGenerationUtil.ts @@ -108,6 +108,7 @@ function mockUnionValues( ) { const schema = schemaMap.get(type); if (schema && schema.enum && schema.enum.length > 0) { + addToSchemaMap(schemaMap, schema.enum[0]); return generateParameterTypeValue( getAccurateTypeName(schema.enum[0]) ?? schema.enum[0], parameterName, @@ -242,6 +243,9 @@ function getAccurateTypeName(schema: Schema) { function addToSchemaMap(schemaMap: Map, schema: Schema) { const type = getAccurateTypeName(schema); + if (!type) { + return; + } if ( !schemaMap.has(type) && !["string", "number", "boolean"].includes(schema.type) diff --git a/packages/typespec-ts/test/unit/sample/generateSampleContent.spec.ts b/packages/typespec-ts/test/unit/sample/generateSampleContent.spec.ts index a7aa36aacf..b852f60741 100644 --- a/packages/typespec-ts/test/unit/sample/generateSampleContent.spec.ts +++ b/packages/typespec-ts/test/unit/sample/generateSampleContent.spec.ts @@ -67,6 +67,45 @@ describe("Integration test for mocking sample", () => { assert.deepEqual(JSON.parse(mockStr!), res); }); + it("anonymous model", async () => { + const schemaMap = await emitSchemasFromTypeSpec(` + model Test { + prop: string; + test: { + prop: string[]; + foo?: int32; + bar: { + prop: string; + baz: boolean[]; + }[]; + baz: Record<{ t: string}>; + unionTest: { a: string } | { b: string }; + } + } + @route("/models") + @get + op getModel(@body body: Test): void; + `); + const mockStr = generateParameterTypeValue( + "Test", + "input", + buildSchemaObjectMap({ schemas: schemaMap } as RLCModel), + new Set() + ); + // console.log(mockStr); + const res = { + prop: "{Your prop}", + test: { + prop: ["{Your prop}"], + foo: 123, + bar: [{ prop: "{Your prop}", baz: [true] }], + baz: { key: { t: "{Your t}" } }, + unionTest: { a: "{Your a}" } + } + }; + assert.deepEqual(JSON.parse(mockStr!), res); + }); + describe("complex model", () => { describe("object", () => { it("self-referenced model", async () => {