Giter Site home page Giter Site logo

Comments (3)

vearutop avatar vearutop commented on September 24, 2024

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.

metaleap avatar metaleap commented on September 24, 2024

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.

metaleap avatar metaleap commented on September 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.