Skip to content

OpenAPIv3: count different validation rules as different types #3640

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

Conversation

isaacseymour
Copy link
Contributor

This replicates an issue we're seeing in the OpenAPIv3 generator, where if two request bodies are structurally similar (i.e. same primitive types), but not identical (in this example both have a single string attribute with the same name, but with different enum validations), the request body for those two methods points at the same type schema.

There's a (previously-failing) test, where:

  • two types which are 'similar' (both have a string-type attr called my_attr), but not identical (they are different enums)
  • two different services with the same method, each referencing different types

The resulting OpenAPIv3 schema generates a single
MethodEnumRequestBody type, and uses it for the requestBody type of both methods.

This is incorrect, since the two methods use a different enum!

The fix here is to update the hashAttribute method to hash the .Validation of the attribute, if it is set 🎉

This replicates an issue we're seeing in the OpenAPIv3 generator, where
if two request bodies are structurally similar (i.e. same primitive
types), but not identical (in this example both have a single string
attribute with the same name, but with different enum validations), the
request body for those two methods points at the same type schema.

There's a (previously-failing) test, where:
- two types which are 'similar' (both have a string-type attr called
  `my_attr`), but not identical (they are different enums)
- two different services with the same method, each referencing
  different types

The resulting OpenAPIv3 schema generates a single
`MethodEnumRequestBody` type, and uses it for the `requestBody` type of
both methods.

This is incorrect, since the two methods use a different enum.

The fix here is to update the `hashAttribute` method to hash the
`.Validation` of the attribute, if it is set 🎉
Copy link
Member

@raphael raphael left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for doing this, makes a lot of sense. The algo might need a bit of tweaking before we can merge it.

var _ = Service("svc_enum_1", func() {
Method("method_enum", func() {
Payload(func() {
Reference(T1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Reference required to repro the bug? would it also happen by simply doing Payload(T1)?

if val.Pattern != "" {
parts = append(parts, hashString(val.Pattern, h))
}
if val.MinLength != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This algo may require a bit of tweaking, as it stands the following two validations would result in the same hash:

Pattern("1")

and

MinLength(1)

Obviously there are many other combinations that would result in the same hash being generated. Maybe we need to use something like https://github.com/gohugoio/hashstructure,

Might also be good to add the example above (and maybe others) to tests.

@raphael
Copy link
Member

raphael commented Feb 3, 2025

Superseded by #3642

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants