guardrail-dev / guardrail-dev/guardrail

Support default values in generated language for properties in schemas

Open
#366 2 comments 0 reactions 0 assignees View on GitHub
core enhancement good first issue help wanted
Dominant language
Scala
Stars
541
Forks
138
PR merge metrics
No merged PRs in 30d

Description

Similar to https://github.com/twilio/guardrail/issues/97

Currently you can specify a default value in your spec for a schema's property if that property's type does not define a specific language type (for example using `x-jvm-type`), and as long as the default value is a static value. For example:

```yaml
components:
schemas:
Thing:
type: object
required:
- foo
properties:
foo:
type: string
bar:
type: string
default: "default-value"
```
And this will generate in scala for example:
```scala
case class Thing(foo: String, bar: String = "default-value")
```

However this breaks down if you are using specific type for a language such as `x-jvm-type`, for example:

```yaml
components:
schemas:
Thing:
type: object
required:
- foo
properties:
foo:
type: string
date_created:
type: string
x-jvm-type: java.time.ZonedDateTime
default: "ZonedDateTime.now()"
```
And this will generate in scala for example:
```scala
case class Thing(foo: String, dateCreated: ZonedDateTime = "ZonedDateTime.now()")
```

It would be awesome if you could provide default values for properties in a schema to go with the language specific extension swagger properties (ie `x-jvm-type`). Maybe something like:
```yaml
components:
schemas:
Thing:
type: object
required:
- foo
properties:
foo:
type: string
date_created:
type: string
x-jvm-type: java.time.ZonedDateTime
x-jvm-default: ZonedDateTime.now()
```
And this will generate in scala for example:
```scala
case class Thing(foo: String, dateCreated: ZonedDateTime = ZonedDateTime.now())
```

Thoughts?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.