danielgtaylor / danielgtaylor/huma
I'm using the huma discriminator setup. Is there a way I can specify a default value for the discriminator property name and have Huma support it?
- Dominant language
- Go
- Stars
- 4.4k
- Forks
- 285
- Avg merge
- 40m
- Merged PRs (30d)
- 1
Description
See below openapi spec. type is the discriminator property. I'm wondering if it is possible we can specify a default discriminator value so for example default: `dog` in this case. Proposed behavior is if the user does not specify the type, we validate the schema against the the Dog schema.
```
openapi: 3.0.0
info:
title: Discriminator Default Property Name Example
version: 1.0.0
paths: {}
components:
schemas:
Pet:
type: object
# The discriminator specifies which property determines the schema
discriminator:
propertyName: type # This is the property name we want to potentially default
mapping:
# Correct: map the discriminator value to a $ref to the schema
dog: '#/components/schemas/Dog'
cat: '#/components/schemas/Cat'
# 'oneOf' indicates that the object must be valid against exactly one of the listed schemas
oneOf:
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'
properties:
name:
type: string
description: The name of the pet.
example: Buddy
Dog:
type: object
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
breed:
type: string
description: The breed of the dog.
example: Golden Retriever
barks:
type: boolean
default: true
description: Does the dog bark?
required:
- breed
Cat:
type: object
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
color:
type: string
description: The color of the cat.
example: Tabby
meows:
type: boolean
default: true
description: Does the cat meow?
required:
- color
```
I feel doing this will be useful because if you think of the natural progression of how a resource schema grows this is quite common.
- initial version, resource will only have 1 type so a type was not added
- next version, new type needs to be added so a type is necessary
Now if the type field becomes required, it becomes a breaking change for the API.
But if the type field is optional, the new type can be added in a non breaking way.
Contributor guide
No contributing guide indexed for this repository
Research direction
The issue provides an OpenAPI discriminator example but names no Huma files or tests. Start by locating Huma's discriminator and schema-validation handling, then review existing tests for discriminator behavior. Done means an agreed default discriminator property value can select the Dog schema when the type is omitted, with coverage for the fallback and existing Cat/Dog cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100