Closed jpalawaga closed 4 months ago
Thank you for raising this issue, please try latest version v0.2.64
.
I'd like to be able to define this in code (for reuse) rather than as a struct tag (i.e make our actual inputs/outputs match the documentation).
With a new version you should be able to define default on a type using Preparer
, Exposer
or RawExposer
.
https://github.com/swaggest/jsonschema-go?tab=readme-ov-file#implementing-interfaces-on-a-type
Hi @vearutop ! thanks for the quick fix.
In my project, I'm still having a bit of issues with this. The problem lies in the jsonschema-go. Specifically, it seems like there is an issue when the custom type appears as a part of more than one variable definition in the request payload.
What I'm seeing (I apologies I snippet for this yet, I'll try and construct one, but for now I'll describe):
reflect()
.reflectDefer
, and cache the reference. keepType
is true, so the type is promotedI think the solution is to change the code reference above to pull the cached object from rc.definitions
instead of rc.definitionRefss
^ PR open with updated test and code change
Describe the bug I'm trying to build an enum type that can be used for input. As a part of this, I'd like to be able to set a default value for the enum field as well. I'd like to be able to define this in code (for reuse) rather than as a struct tag (i.e make our actual inputs/outputs match the documentation).
However, when I try to use a custom type with the default struct tags, it seems as though the parser expects a json string/object rather than a string value. You can use a json string to get past this, but then default does something weird. Concrete example below.
To Reproduce
Observed behavior
Expected behavior The API returns
all
.Side note: you can change this so it reads
default:"\"all\""
-- then the input validation passes, but your default ends up as the quoted json string"all"
, notall
(i.e. it is not possible to produce the valid enum option).--
I'm happy to make a contribution here if you can point me in the right direction. I did step through the code a bit. I tried Implementing a TextUn/Marshaller so that maybe the type would properly get set as a string in
jsonschema-go/reflect.go
, but that didn't seem to make anything better on the version I was using. I believe it's because string is added as "a" type, rather than "the" SimpleType, which causes thecheckInlineValue
to fall into thedefault
rather thanString
case (where it does ajson.Unmarshal
)