Skip to content

Commit

Permalink
Fix issue with name collisions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Nov 29, 2023
1 parent c8fe779 commit f251fe0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
10 changes: 6 additions & 4 deletions packages/openapi3/src/visibility-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ export function resolveVisibilityUsage(
if (!omitUnreachableTypes) {
const trackType = (type: Type) => {
if (!usages.has(type)) {
navigateReferencedTypes(type, Visibility.Read, (type, vis) =>
trackUsageExact(usages, type, vis)
);
// add usage for all unreferenced types and those referenced by them
navigateReferencedTypes(type, Visibility.All, (type, vis) => {
trackUsageExact(usages, type, vis);
});
}
};
navigateTypesInNamespace(root, {
Expand All @@ -47,6 +48,8 @@ export function resolveVisibilityUsage(
enum: trackType,
union: trackType,
});
const unreachableTypes = new Set<Type>(usages.keys());
reachableTypes.forEach((name) => unreachableTypes.delete(name));
}
return {
getUsage: (type: Type) => {
Expand All @@ -65,7 +68,6 @@ export function resolveVisibilityUsage(
function addUsagesInContainer(
program: Program,
metadataInfo: MetadataInfo,

type: OperationContainer,
usages: Map<Type, Set<Visibility>>
) {
Expand Down
3 changes: 3 additions & 0 deletions packages/openapi3/test/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ describe("openapi3: metadata", () => {

it("prioritizes read visibility when referenced and unreferenced models share schemas", async () => {
const res = await openApiFor(`
@service({})
namespace TestNs;
model Shared {
@visibility("create", "update") password: string;
prop: string;
Expand Down
4 changes: 0 additions & 4 deletions packages/samples/specs/visibility/visibility.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ namespace Hello {
@post op create(@body person: WritablePerson): ReadablePerson;
@put op update(@body person: WriteableOptionalPerson): ReadablePerson;

@route("/hello/optional")
@get
op getOptional(): OptionalPerson;

@TypeSpec.Rest.listsResource(Person)
op list(): ListResult<Person>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/PersonUpdate'
/hello/hello/optional:
get:
operationId: Hello_getOptional
parameters: []
responses:
'200':
description: The request has succeeded.
content:
application/json:
schema:
$ref: '#/components/schemas/OptionalPerson'
/hello/{id}:
get:
operationId: Hello_read
Expand Down Expand Up @@ -114,6 +103,8 @@ components:
id:
type: string
readOnly: true
secret:
type: string
name:
type: string
test:
Expand All @@ -123,7 +114,7 @@ components:
relatives:
type: array
items:
$ref: '#/components/schemas/PersonRelative'
$ref: '#/components/schemas/PersonRelativeReadOrCreateOrUpdateOrDeleteItem'
Person:
type: object
required:
Expand Down Expand Up @@ -185,6 +176,31 @@ components:
type: array
items:
$ref: '#/components/schemas/PersonRelativeCreateOrUpdateItem'
PersonReadOrCreateOrUpdateOrDeleteItem:
type: object
required:
- id
- secret
- name
- test
- other
- relatives
properties:
id:
type: string
readOnly: true
secret:
type: string
name:
type: string
test:
type: string
other:
type: string
relatives:
type: array
items:
$ref: '#/components/schemas/PersonRelativeReadOrCreateOrUpdateOrDeleteItem'
PersonRelative:
type: object
required:
Expand Down Expand Up @@ -215,6 +231,16 @@ components:
$ref: '#/components/schemas/PersonCreateOrUpdateItem'
relationship:
type: string
PersonRelativeReadOrCreateOrUpdateOrDeleteItem:
type: object
required:
- person
- relationship
properties:
person:
$ref: '#/components/schemas/PersonReadOrCreateOrUpdateOrDeleteItem'
relationship:
type: string
PersonRelativeUpdateItem:
type: object
required:
Expand Down

0 comments on commit f251fe0

Please sign in to comment.