guardrail-dev / guardrail-dev/guardrail

support for number backed enums

Open
#1,612 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Scala
Stars
541
Forks
138
PR merge metrics
No merged PRs in 30d

Description

according to
https://stackoverflow.com/questions/66465888/how-to-define-enum-mapping-in-openapi
openapi 3.1 allows the following format for defining number backed enums

```
openapi: 3.1.0
info:
title: test
servers:
- url: /

components:
schemas:

Severity:
type: integer
oneOf:
- title: HIGH
const: 2
description: An urgent problem
- title: MEDIUM
const: 1
- title: LOW
const: 0
description: Can wait forever
```

it fails with the following error

```
Error:Unknown type for the following structure (No type definition, class: io.swagger.v3.oas.models.media.JsonSchema, .components.schemas.Severity):
Tracker(class JsonSchema {
class Schema {
type: [integer]
}
}, Vector(.components, .schemas, .Severity))

```
One could generate something similar to this instead

```
sealed abstract class Severity(val code: Int, val value: String) {
override def toString: String = value
}
object Severity {
object members {
case object HIGH extends Severity(2, "HIGH")
case object MEDIUM extends Severity(1, "MEDIUM")
case object LOW extends Severity(0, "LOW")
}
val HIGH: Severity = members.HIGH
val MEDIUM: Severity = members.MEDIUM
val LOW: Severity = members.LOW

val values = Vector(HIGH, MEDIUM, LOW)

}

```

Contributor guide

Open the contributing guide

Research direction

The issue names no repository files or tests; start by tracing the OpenAPI schema-parsing and Scala enum-generation entry points. Reproduce the supplied OpenAPI 3.1 Severity schema, then verify that numeric const values are accepted and generation produces the requested sealed Scala representation.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, scala
Domain
backend-api-design, devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.