Skip to content

Commit 8944be4

Browse files
feat: Adding constant handling (#220)
* Adding constant handling. * Add Kusmeroglu as contributor --------- Co-authored-by: Fabien Bernard <[email protected]>
1 parent ecf90cd commit 8944be4

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

.all-contributorsrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
"avatar_url": "https://avatars.githubusercontent.com/u/22816887?v=4",
106106
"profile": "https://github.com/dan-cooke",
107107
"contributions": ["bug", "code"]
108+
},
109+
{
110+
"login": "Kusmeroglu",
111+
"name": "Linden Wright",
112+
"avatar_url": "https://avatars.githubusercontent.com/u/1638544?v=4",
113+
"profile": "https://github.com/Kusmeroglu",
114+
"contributions": ["code"]
108115
}
109116
],
110117
"contributorsPerLine": 7

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
303303
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Cellule"><img src="https://avatars.githubusercontent.com/u/4157103?v=4?s=100" width="100px;" alt="Michael &quot;Mike&quot; Ferris"/><br /><sub><b>Michael &quot;Mike&quot; Ferris</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=Cellule" title="Code">💻</a></td>
304304
<td align="center" valign="top" width="14.28%"><a href="https://github.com/rajzik"><img src="https://avatars.githubusercontent.com/u/10364836?v=4?s=100" width="100px;" alt="Jan Šilhan"/><br /><sub><b>Jan Šilhan</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/issues?q=author%3Arajzik" title="Bug reports">🐛</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=rajzik" title="Code">💻</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=rajzik" title="Documentation">📖</a></td>
305305
<td align="center" valign="top" width="14.28%"><a href="https://github.com/dan-cooke"><img src="https://avatars.githubusercontent.com/u/22816887?v=4?s=100" width="100px;" alt="Daniel Cooke"/><br /><sub><b>Daniel Cooke</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/issues?q=author%3Adan-cooke" title="Bug reports">🐛</a> <a href="https://github.com/fabien0102/openapi-codegen/commits?author=dan-cooke" title="Code">💻</a></td>
306+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Kusmeroglu"><img src="https://avatars.githubusercontent.com/u/1638544?v=4?s=100" width="100px;" alt="Linden Wright"/><br /><sub><b>Linden Wright</b></sub></a><br /><a href="https://github.com/fabien0102/openapi-codegen/commits?author=Kusmeroglu" title="Code">💻</a></td>
306307
</tr>
307308
</tbody>
308309
</table>

plugins/typescript/src/core/schemaToTypeAliasDeclaration.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ describe("schemaToTypeAliasDeclaration", () => {
5959
expect(printSchema(schema)).toBe("export type Test = number[];");
6060
});
6161

62+
it("should generate a string const", () => {
63+
const schema: SchemaObject = {
64+
const: "CONSTANT",
65+
title: "Test",
66+
};
67+
68+
expect(printSchema(schema)).toBe('export type Test = "CONSTANT";');
69+
});
70+
6271
it("should generate enums (strings)", () => {
6372
const schema: SchemaObject = {
6473
type: "string",

plugins/typescript/src/core/schemaToTypeAliasDeclaration.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ export const getType = (
189189
schema.type = "array";
190190
}
191191

192+
// Handle string constant
193+
if (schema.const && !schema.type) {
194+
return f.createLiteralTypeNode(f.createStringLiteral(schema.const));
195+
}
196+
192197
switch (schema.type) {
193198
case "null":
194199
return f.createLiteralTypeNode(f.createNull());

0 commit comments

Comments
 (0)