From 3ac7a5c33b3a0fa0363cf2a17192a874ba266925 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Wed, 1 Nov 2023 12:47:33 -0500 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFUpdate=20naming=20logic.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/openapi3/src/openapi.ts | 44 ++++++++++++++++++- .../@typespec/openapi3/openapi.yaml | 10 ++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/packages/openapi3/src/openapi.ts b/packages/openapi3/src/openapi.ts index fcd4634f316..56fd766bbb0 100644 --- a/packages/openapi3/src/openapi.ts +++ b/packages/openapi3/src/openapi.ts @@ -1311,7 +1311,48 @@ function createOAPIEmitter( for (const declaration of declarations) { schemas[declaration.name] = declaration.value as any; } - } + + // TODO: The old logic + // Emit the processed schemas. Only now can we compute the names as it + // depends on whether we have produced multiple schemas for a single + // TYPESPEC type. + // for (const group of processedSchemas.values()) { + // for (const [visibility, processed] of group) { + // let name = getOpenAPITypeName(program, processed.type, typeNameOptions); + // if (group.size > 1) { + // const suffix = getVisibilitySuffix(visibility, Visibility.All); + // // ignore plain "Read" suffix since it is the canonical visibility + // // for referenced schemas + // if (suffix !== "Read") { + // name += suffix; + // } + // } + // checkDuplicateTypeName(program, processed.type, name, root.components!.schemas); + // processed.ref.value = "#/components/schemas/" + encodeURIComponent(name); + // if (processed.schema) { + // root.components!.schemas![name] = processed.schema; + // } + // } + // } + } + + // TODO: Old logic + // function processSchemas() { + // // Process pending schemas. Note that getSchemaForType may pull in new + // // pending schemas so we iterate until there are no pending schemas + // // remaining. + // while (pendingSchemas.size > 0) { + // for (const [type, group] of pendingSchemas) { + // for (const [visibility, pending] of group) { + // processedSchemas.getOrAdd(type, visibility, () => ({ + // ...pending, + // schema: getSchemaForType(type, visibility), + // })); + // } + // pendingSchemas.delete(type); + // } + // } + // } function processUnreferencedSchemas() { const addSchema = (type: Type) => { @@ -1321,6 +1362,7 @@ function createOAPIEmitter( !shouldInline(program, type) ) { getSchemaOrRef(type, Visibility.Read); + } // TODO: This was the old fix... // if (!processedSchemas.has(type) && !paramModels.has(type) && !shouldInline(program, type)) { // getSchemaOrRef(type, Visibility.All); diff --git a/packages/samples/test/output/visibility/@typespec/openapi3/openapi.yaml b/packages/samples/test/output/visibility/@typespec/openapi3/openapi.yaml index affea0069bc..a4df1066b8a 100644 --- a/packages/samples/test/output/visibility/@typespec/openapi3/openapi.yaml +++ b/packages/samples/test/output/visibility/@typespec/openapi3/openapi.yaml @@ -114,7 +114,7 @@ components: relatives: type: array items: - $ref: '#/components/schemas/PersonRelativeReadOrCreateOrUpdateOrDeleteOrQueryItem' + $ref: '#/components/schemas/PersonRelativeItem' Person: type: object required: @@ -176,7 +176,7 @@ components: type: array items: $ref: '#/components/schemas/PersonRelativeCreateOrUpdateItem' - PersonReadOrCreateOrUpdateOrDeleteOrQueryItem: + PersonItem: type: object required: - id @@ -200,7 +200,7 @@ components: relatives: type: array items: - $ref: '#/components/schemas/PersonRelativeReadOrCreateOrUpdateOrDeleteOrQueryItem' + $ref: '#/components/schemas/PersonRelativeItem' PersonRelative: type: object required: @@ -231,14 +231,14 @@ components: $ref: '#/components/schemas/PersonCreateOrUpdateItem' relationship: type: string - PersonRelativeReadOrCreateOrUpdateOrDeleteOrQueryItem: + PersonRelativeItem: type: object required: - person - relationship properties: person: - $ref: '#/components/schemas/PersonReadOrCreateOrUpdateOrDeleteOrQueryItem' + $ref: '#/components/schemas/PersonItem' relationship: type: string PersonRelativeUpdateItem: