Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: define how to handle reserved keywords #25

Closed
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
55 changes: 55 additions & 0 deletions docs/handling reserved keywords.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Handling reserved keywords

JSON Schema have no restrictions on property names. However in programming languages, there often is reserved keywords that properties cannot be named.

For example given the following JSON Schema:

```json
{
"$schema": "https://json-schema.org/draft/2020-12/idl-schema",
"name": "SomeTitle",
"type": "object",
"properties": {
"return": {
"type": "string"
}
}
}
```

The optimal output to something like TypeScript, would probably be the following:

```ts
class SomeTitle {
return: string;
}
```

However, TypeScript don't allow such a property name, because it collide with the reserved keyword `return` used to return data from functions.

## Solutions
There are no specific solutions that we enforce, it is up to the implementor.

To make your life easier, here is the list of keywords that are reserved:
- [TypeScript](reserved%20keywords/typescript.md)
- [C#](reserved%20keywords/csharp.md)
- [javascript](reserved%20keywords/javascript.md)
- [java](reserved%20keywords/java.md)

If the reserved keyword list your are looking for is not present, please provide a PR!

### Adding something extra to the property name
One of the solutions is to pre- or post-fix some kind of word to the rendered property name.

For example prefixing `reserved` to a property name.

```ts
class SomeTitle {
reservedReturn: string;
}
```

This means you will end up with two names for the property. The one rendered, and the one needed for serializing the data model (because otherwise validating the serialized data wont validate correctly against the JSON Schema).

### Throw an error
The easiest solution is to throw an error whenever they are encountered, and stopping the generation of models.
82 changes: 82 additions & 0 deletions docs/reserved keywords/csharp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
## Reserved keywords for Go
We do not guarantee this list of keywords are complete, however, if you find anything missing, please provide a PR!

```
abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
enum
event
explicit
extern
false
finally
fixed
float
for
foreach
goto
if
implicit
in
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
uint
ulong
unchecked
unsafe
ushort
using
virtual
void
volatile
while
```
54 changes: 54 additions & 0 deletions docs/reserved keywords/java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Reserved keywords for Java
We do not guarantee this list of keywords are complete, however, if you find anything missing, please provide a PR!

```
abstract
continue
for
new
switch assert
default
goto
package
synchronized
boolean
do
if
private
this
break
double
implements
protected
throw
byte
else
import
public
throws
case
enum
instanceof
return
transient
catch
extends
int
short
try
char
final
interface
static
void
class
finally
long
strictfp
volatile
const
float
native
super
while
```
Loading