Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]: Add support of propertyNames. #638

Open
superstas opened this issue Nov 5, 2024 · 0 comments
Open

[feature]: Add support of propertyNames. #638

superstas opened this issue Nov 5, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@superstas
Copy link
Contributor

Hi there,

I have the following types

main.go

var (
	_ huma.SchemaTransformer = Key("")
	_ huma.SchemaTransformer = Value("")
)

type (
	Key   string
	Value string
)

func (k Key) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema {
	min, max := 1, 10
	s.Description = "A key for a map."
	s.MinLength = &min
	s.MaxLength = &max
	return s
}

func (v Value) TransformSchema(r huma.Registry, s *huma.Schema) *huma.Schema {
	min, max := 1, 32
	s.Description = "A value for a map."
	s.MinLength = &min
	s.MaxLength = &max

	return s
}

type Input struct {
	Body struct {
		Values map[Key]Value `json:"values" `
	}
}

type Output struct {
	Body struct {
		Message string `json:"message"`
	}
}

func addRoutes(api huma.API) {
	huma.Post(api, "/input", func(ctx context.Context, input *Input) (*Output, error) {
		resp := &Output{}
		resp.Body.Message = "It works!"
		return resp, nil
	})
}

One parameter, values, is a map of Key and Value.
Key and Value have validation rules ( in TransfromSchema ).

The issue.

  1. Key validation doesn't work.

main_test.go

func TestUserCreate(t *testing.T) {
	_, api := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0"))
	addRoutes(api)

	_ = api.Post("/input", map[string]any{"values": map[string]any{"": "value"}})
}

Temporary workaround: implementation of huma.Resolver/huma.ResolverWithPath interface.

  1. Generated OAS doesn't have a Key schema.
    InputBody:
      additionalProperties: false
      properties:
        $schema:
          description: A URL to the JSON Schema for this object.
          examples:
            - https://example.com/schemas/InputBody.json
          format: uri
          readOnly: true
          type: string
        values:
          additionalProperties:
            description: A value for a map.
            maxLength: 32
            minLength: 1
            type: string
          type: object
      required:
        - values
      type: object

Value type is represented by additionalProperties.

Key may be represented by propertyNames like

...
       values:
         propertyNames:
           type: string
           maxLength: 10
           minLength: 1
         additionalProperties:
           description: A value for a map.
           maxLength: 32
           minLength: 1
           type: string
         type: object
...

From what I've checked in the source code, propertyNames is not supported yet.

Could you please briefly describe what needs to be considered for the implementation in the source code ( if someone wants to contribute it )?

Thank you.

@danielgtaylor danielgtaylor added the enhancement New feature or request label Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants