-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: token api simplification (#4600)
## About the changes We found a problem generating the Go SDK client for the tokens API that makes use of `oneOf`, combined with `allOf`. The generator doesn't know how to map this type and leaves it as an object. This PR simplifies the spec and therefore the code generated after it
- Loading branch information
1 parent
53f90d3
commit 19ec66a
Showing
2 changed files
with
60 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { JSONSchema } from 'json-schema-to-ts'; | ||
|
||
// this function simplifies simple schemas and return allOf schema if it | ||
// doesn't know how to simplify it. It's a proof of concept but it can be extended | ||
export function mergeAllOf(a: JSONSchema, b: JSONSchema): JSONSchema { | ||
if (typeof a !== 'boolean' && typeof b !== 'boolean') { | ||
const { | ||
required: aRequired, | ||
properties: aProperties, | ||
type: aType, | ||
...aRest | ||
} = a; | ||
const { | ||
required: bRequired, | ||
properties: bProperties, | ||
type: bType, | ||
...bRest | ||
} = b; | ||
if ( | ||
Object.keys(aRest).length === 0 && | ||
Object.keys(bRest).length === 0 && | ||
aType === 'object' && | ||
bType === 'object' | ||
) { | ||
return { | ||
required: [...(aRequired ?? []), ...(bRequired ?? [])], | ||
type: 'object', | ||
properties: { ...aProperties, ...bProperties }, | ||
}; | ||
} | ||
} | ||
return { | ||
allOf: [a, b], | ||
}; | ||
} | ||
|
||
export function mergeAllOfs(schemas: JSONSchema[]): JSONSchema { | ||
if (schemas.length === 1) { | ||
return schemas[0]; | ||
} | ||
const [a, b, ...rest] = schemas; | ||
return mergeAllOfs([mergeAllOf(a, b), ...rest]); | ||
} |