Comments (3)
I tried to reproduce and could not:
func TestReflector_Reflect_customTime(t *testing.T) {
type MyTime time.Time
type MyPtrTime *time.Time
type MyStruct struct {
T1 MyTime `json:"t1"`
T2 *MyTime `json:"t2"`
T3 *MyPtrTime `json:"t3"`
}
r := jsonschema.Reflector{}
s, err := r.Reflect(MyStruct{})
require.NoError(t, err)
assertjson.EqMarshal(t, `{
"definitions":{"JsonschemaGoTestMyTime":{"type":"object"}},
"properties":{
"t1":{"$ref":"#/definitions/JsonschemaGoTestMyTime"},
"t2":{"$ref":"#/definitions/JsonschemaGoTestMyTime"},
"t3":{"type":["null","string"],"format":"date-time"}
},
"type":"object"
}`, s)
}
Could you share some more details about the reflected structure?
from jsonschema-go.
Wow, thanks for checking! This is actually from openapi-go
usage, I should have added (but throws from jsonschema-go
). And sorry for the lack of repro last night. But fresh morning, now here's a repro_test.go that will reliably provoke the above-quoted panic
at my end, and hopefully at yours too:
package repro_test
import (
"reflect"
"testing"
"time"
"github.com/swaggest/jsonschema-go"
"github.com/swaggest/openapi-go"
"github.com/swaggest/openapi-go/openapi31"
)
type yodb_I64 int64
type yodb_DateTime time.Time
type Return[T any] struct{ Result T }
type Post struct {
Id yodb_I64 // note: panic occurs also _with_ json: struct-field tags placed here
DtMade *yodb_DateTime // as well as without — so, not a factor
}
func Test_Repro(t *testing.T) {
oarefl := openapi31.NewReflector()
oarefl.Spec.Info.WithTitle("kaffe.local")
oarefl.JSONSchemaReflector().DefaultOptions = append(oarefl.JSONSchemaReflector().DefaultOptions, jsonschema.ProcessWithoutTags)
{
var dummy_in Post
var dummy_out Return[yodb_I64]
ty_args, ty_ret := reflect.TypeOf(dummy_in), reflect.TypeOf(dummy_out)
op, err := oarefl.NewOperationContext("POST", "/_/postNew")
if err != nil {
t.Fatal(err)
}
op.AddReqStructure(reflect.New(ty_args).Elem().Interface(), openapi.WithContentType("application/json"))
op.AddRespStructure(reflect.New(ty_ret).Elem().Interface(), openapi.WithHTTPStatus(200))
// note on above 2 `Elem()`s: panic occurs with or without them
if err = oarefl.AddOperation(op); err != nil {
t.Fatal(err)
}
}
src_json, err := oarefl.Spec.MarshalJSON()
if err != nil {
t.Fatal(err)
}
t.Log(string(src_json))
}
func (me *yodb_DateTime) UnmarshalJSON(data []byte) error {
return ((*time.Time)(me)).UnmarshalJSON(data)
}
func (me *yodb_DateTime) MarshalJSON() ([]byte, error) { return ((*time.Time)(me)).MarshalJSON() }
With [email protected]
and [email protected]
according to stacktrace.
from jsonschema-go.
Rolling my own stuff in the meanwhile, I ran into the seemingly-same issue, and looks like it kinda evaporated by changing from this pattern:
func dummyOf(ty reflect.Type, level int, typesDone map[reflect.Type]bool) *reflect.Value {
dummy := reflect.New(ty).Elem()
for ty.Kind() == reflect.Pointer {
ty = ty.Elem()
dummy = dummy.Elem()
}
myAssert(dummy.IsValid()) // panics on hitting the culprit-type of this Issue thread
// ...
to this pattern instead:
func dummyOf(ty reflect.Type, level int, typesDone map[reflect.Type]bool) *reflect.Value {
if ty.Kind() == reflect.Pointer {
return dummyOf(ty.Elem(), level, typesDone)
}
dummy := reflect.New(ty).Elem()
myAssert(dummy.IsValid()) // no more panic
// ...
from jsonschema-go.
Related Issues (19)
- Interception occurs before PrepareJSONSchema HOT 2
- implementations of `encoding.TextMarshaler` render as a union of the base type and string, instead of just string HOT 2
- Struct tags for array of enum values HOT 2
- Add const struct tag reflection HOT 1
- support example tag on slice field. HOT 4
- panic when get slice type struct
- Example tags show problems HOT 1
- example show problems HOT 1
- How to validate input unmarshal to a struct HOT 3
- Implementing TextMarshaler overrides my Schema HOT 1
- It is definitions or $defs? HOT 1
- Any chance to have access to the actual values of a types while implementing interfaces? HOT 2
- Reflector 's Mapper callback HOT 2
- Custom schemas properties using tags HOT 2
- default tag does not work for arrays
- should InterceptType be called after options applied? HOT 1
- How to create JSON schema based on a JSON data? HOT 2
- Duplicate Fields have empty definitions with `InlineRefs` enabled HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonschema-go.