Giter Site home page Giter Site logo

Comments (5)

B87 avatar B87 commented on June 22, 2024

Hi! I'm facing the same issue, represent a google UUID as a string on my schema.

What is the best way of doing it? I see that jsonschema.InterceptType has been deprecated

from openapi-go.

danicc097 avatar danicc097 commented on June 22, 2024

I append jsonschema.InterceptSchema to openapi3.Reflector's DefaultOptions with

// jsonschema.InterceptSchema(func(params jsonschema.InterceptSchemaParams) (stop bool, err error)
t := params.Schema.ReflectType
if t.Kind() == reflect.Ptr {
  t = t.Elem()
}
if t == reflect.TypeOf(uuid.New()) {
  params.Schema.Type = &jsonschema.Type{SimpleTypes: pointers.New(jsonschema.String)}
  params.Schema.Pattern = pointers.New("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
  params.Schema.Items = &jsonschema.Items{}
  // oapi-codegen
  params.Schema.ExtraProperties = map[string]any{
	  "x-go-type": "uuid.UUID",
	  "x-go-type-import": map[string]any{
		  "name": "uuid",
		  "path": "github.com/google/uuid",
	  },
	  "x-is-generated": true,
  }
}

using:

  • github.com/swaggest/jsonschema-go v0.3.64
  • github.com/swaggest/openapi-go v0.2.44

from openapi-go.

B87 avatar B87 commented on June 22, 2024

Thank you so much @danicc097, your approach worked like a charm. I noticed that when doing this you will need to add an example manually since the struct example for path parameters won't be used on the spec generation.

This is how my code looks like now:

reflector.DefaultOptions = append(reflector.DefaultOptions, jsonschema.InterceptSchema(
		func(params jsonschema.InterceptSchemaParams) (stop bool, err error) {
			t := params.Schema.ReflectType
			if t.Kind() == reflect.Ptr {
				t = t.Elem()
			}
			if t == reflect.TypeOf(uuid.New()) {
				params.Schema.Type = &jsonschema.Type{SimpleTypes: &strSchema}
				params.Schema.Pattern = &uuidPattern
				params.Schema.Items = &jsonschema.Items{}
				// oapi-codegen
				params.Schema.Examples = []interface{}{"cdb15f83-1c5d-4727-98d1-8924ccd1fc01"}
				params.Schema.ExtraProperties = map[string]any{
					"x-go-type": "uuid.UUID",
					"x-go-type-import": map[string]any{
						"name": "uuid",
						"path": "github.com/google/uuid",
					},
					"x-is-generated": true,
				}
			}
			return false, nil
		}))

using:

  • github.com/swaggest/openapi-go v0.2.45
  • github.com/swaggest/jsonschema-go v0.3.64

from openapi-go.

vearutop avatar vearutop commented on June 22, 2024

I'm sorry this issue already took me so long, I'll try to come up with a proper solution soon. In meanwhile, one of the workarounds is to declare desired schema with type mapping:

https://github.com/swaggest/rest/blob/v0.2.61/_examples/advanced-generic-openapi31/router.go#L84-L89

	// Create custom schema mapping for 3rd party type.
	uuidDef := jsonschema.Schema{}
	uuidDef.AddType(jsonschema.String)
	uuidDef.WithFormat("uuid")
	uuidDef.WithExamples("248df4b7-aa70-47b8-a036-33ac447e668d")
	jsr.AddTypeMapping(uuid.UUID{}, uuidDef)

from openapi-go.

B87 avatar B87 commented on June 22, 2024

@vearutop Thanks for the example, your approach looks cleaner. It would be nice to have a native uuid integration but this is good enough for me.

from openapi-go.

Related Issues (20)

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.