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
2 changes: 1 addition & 1 deletion packages/massimo-cli/lib/frontend-openapi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ function generateFrontendImplementationFromOpenAPI ({
function generateTypesFromOpenAPI ({ schema, name, fullRequest, fullResponse, propsOptional, namedSchemas }) {
const camelCaseName = capitalize(camelcase(name))
const { paths } = schema
const generatedOperationIds = []
const generatedOperationIds = new Set()
const operations = Object.entries(paths).flatMap(([path, methods]) => {
return Object.entries(methods).map(([method, operation]) => {
const opId = generateOperationId(path, method, operation, generatedOperationIds)
Expand Down
2 changes: 1 addition & 1 deletion packages/massimo-cli/lib/openapi-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function generateTypesFromOpenAPI ({
const camelcasedName = toJavaScriptName(name)
const capitalizedName = capitalize(camelcasedName)
const { paths } = schema
const generatedOperationIds = []
const generatedOperationIds = new Set()

const operations = Object.entries(paths).flatMap(([path, methods]) => {
let commonParameters = []
Expand Down
2 changes: 1 addition & 1 deletion packages/massimo/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type OpenTelemetry = {
setErrorInSpanClient?: (span: any, err: unknown) => void
}

export function generateOperationId(path: string, method: string, methodMeta: MethodMetaInterface, all: string[]): string
export function generateOperationId(path: string, method: string, methodMeta: MethodMetaInterface, all: string[] | Set<string>): string
export function buildOpenAPIClient<T>(options: BuildOpenAPIClientOptions, openTelemetry?: OpenTelemetry): Promise<T>
export function buildGraphQLClient(options: BuildGraphQLClientOptions, openTelemetry?: OpenTelemetry, logger?: AbstractLogger): Promise<BuildGraphQLClientOutput>
export function hasDuplicatedParameters(methodMeta: MethodMetaInterface): boolean
Expand Down
11 changes: 8 additions & 3 deletions packages/massimo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ function wrapGraphQLClient (url, openTelemetry, logger) {
}

export function generateOperationId (path, method, methodMeta, all) {
const hasOperationId = candidate => all instanceof Set ? all.has(candidate) : all.includes(candidate)
let operationId = null
// use methodMeta.operationId only if it's present AND it is a valid string that can be
// concatenated without converting it
Expand Down Expand Up @@ -385,7 +386,7 @@ export function generateOperationId (path, method, methodMeta, all) {
} else {
let count = 0
let candidate = operationId
while (all.includes(candidate)) {
while (hasOperationId(candidate)) {
if (count === 0) {
// first try with method name
candidate = `${method}${capitalize(operationId)}`
Expand All @@ -397,7 +398,11 @@ export function generateOperationId (path, method, methodMeta, all) {
operationId = candidate
}
operationId = camelCase(operationId)
all.push(operationId)
if (all instanceof Set) {
all.add(operationId)
} else {
all.push(operationId)
}
return operationId
}

Expand Down Expand Up @@ -441,7 +446,7 @@ export async function buildOpenAPIClient (options, openTelemetry) {
dispatcher = dispatcher.compose(interceptors.responseError())
}

const generatedOperationIds = []
const generatedOperationIds = new Set()
for (const path of Object.keys(spec.paths)) {
const pathMeta = spec.paths[path]
let commonParameters = []
Expand Down
14 changes: 14 additions & 0 deletions packages/massimo/test/generateOperationId.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ test('generates name from different path with same method and opeartionId', asyn
equal(fourth, 'getSampleOperationId2')
})

test('supports a Set for efficient operation ID tracking', () => {
const ids = new Set()
const first = generateOperationId('sample-path', 'get', { operationId: 'sampleOperationId' }, ids)
const second = generateOperationId('sample-path2', 'get', { operationId: 'sampleOperationId' }, ids)
const third = generateOperationId('sample-path3', 'get', { operationId: 'sampleOperationId' }, ids)

equal(first, 'sampleOperationId')
equal(second, 'getSampleOperationId')
equal(third, 'getSampleOperationId1')
equal(ids.has(first), true)
equal(ids.has(second), true)
equal(ids.has(third), true)
})

test('generates name with path parameter', async t => {
const opId = generateOperationId('/v3/accounts/{id}', 'get', {}, [])
equal(opId, 'getV3AccountsId')
Expand Down