Skip to content

Commit c543d28

Browse files
Alan-ChaErikWittern
authored andcommitted
Selectively add type names
Signed-off-by: Alan Cha <[email protected]>
1 parent 6cb53fd commit c543d28

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

packages/openapi-to-graphql/lib/preprocessor.js

Lines changed: 9 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/preprocessor.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/types/preprocessing_data.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export declare type PreprocessingData = {
3434
/**
3535
* List of all the used object names to avoid collision
3636
*/
37-
usedOTNames: string[];
37+
usedTypeNames: string[];
3838
/**
3939
* List of data definitions for JSON schemas already used.
4040
*/

packages/openapi-to-graphql/src/preprocessor.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function preprocessOas(
3030
options: InternalOptions
3131
): PreprocessingData {
3232
const data: PreprocessingData = {
33-
usedOTNames: [
33+
usedTypeNames: [
3434
'Query', // Used by OpenAPI-to-GraphQL for root-level element
3535
'Mutation' // Used by OpenAPI-to-GraphQL for root-level element
3636
],
@@ -503,7 +503,7 @@ export function createDataDef(
503503
return existingDataDef
504504
} else {
505505
// Else, define a new name, store the def, and return it
506-
const name = getSchemaName(names, data.usedOTNames)
506+
const name = getSchemaName(names, data.usedTypeNames)
507507

508508
/**
509509
* Store and sanitize the name
@@ -516,13 +516,19 @@ export function createDataDef(
516516
Oas3Tools.storeSaneName(saneName, name, data.saneMap)
517517
)
518518
const iotName = otName + 'Input'
519-
// Add the names to the master list
520-
data.usedOTNames.push(otName)
521-
data.usedOTNames.push(iotName)
522519

523520
// Determine the type of the schema
524521
const type = Oas3Tools.getSchemaType(schema as SchemaObject, data)
525522

523+
// Only add type names if a type will be created
524+
if (type === 'object' || type === 'array' || type === 'enum') {
525+
// Add the names to the master list
526+
data.usedTypeNames.push(otName)
527+
528+
// TODO: selectively add input object type names if they will be created
529+
data.usedTypeNames.push(iotName)
530+
}
531+
526532
const def: DataDefinition = {
527533
preferredName,
528534

packages/openapi-to-graphql/src/types/preprocessing_data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export type PreprocessingData = {
4242
/**
4343
* List of all the used object names to avoid collision
4444
*/
45-
usedOTNames: string[]
45+
usedTypeNames: string[]
4646

4747
/**
4848
* List of data definitions for JSON schemas already used.

packages/openapi-to-graphql/test/oas_3_tools.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ test('Identify allOf as an object', () => {
207207
expect(
208208
Oas3Tools.getSchemaType(schema, {
209209
operations: {},
210-
usedOTNames: [],
210+
usedTypeNames: [],
211211
defs: [],
212212
security: {},
213213
saneMap: {},

0 commit comments

Comments
 (0)