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

Error with UnmarshalYAML() using http/bearer auth scheme #100

Closed
sudorandom opened this issue Mar 5, 2024 · 9 comments · Fixed by #101
Closed

Error with UnmarshalYAML() using http/bearer auth scheme #100

sudorandom opened this issue Mar 5, 2024 · 9 comments · Fixed by #101

Comments

@sudorandom
Copy link

sudorandom commented Mar 5, 2024

Describe the bug
I get an error when unmarshaling an openapi v3 spec that includes "bearer" as the scheme and has a bearerFormat value:

oneOf constraint failed for SecuritySchemeOrReference with 0 valid results: map[Reference:required key missing: $ref SecurityScheme:oneOf constraint failed for SecurityScheme with 2 valid results: map[APIKey:required key missing: name MutualTLS:bad const value for "type" ("mutualTLS" expected, "http" received) Oauth2:required key missing: flows Oidc:required key missing: openIdConnectUrl]]

To Reproduce
Run github.com/swaggest/openapi-go/openapi31.Schema.Unmarshal() with this input:

openapi: 3.1.0
info:
  title: MyProject
  description: "My Project Description"
  version: v1.0.0
# 1) Define the security scheme type (HTTP bearer)
components:
  securitySchemes:
    bearerAuth: # arbitrary name for the security scheme
      type: http
      scheme: bearer
      bearerFormat: JWT # optional, arbitrary value for documentation purposes
# 2) Apply the security globally to all operations
security:
  - bearerAuth: [] # use the same name as above

Go Playground: https://go.dev/play/p/TpF1raEIANk

Expected behavior
I expected the scheme to parse without an error and have these assertions hold true of the resulting schema:

s.Components.SecuritySchemes["bearerAuth"].SecurityScheme.HTTP != nil
s.Components.SecuritySchemes["bearerAuth"].SecurityScheme.HTTP.Scheme == "bearer"
s.Components.SecuritySchemes["bearerAuth"].SecurityScheme.HTTPBearer != nil
s.Components.SecuritySchemes["bearerAuth"].SecurityScheme.HTTPBearer.BearerFormat == "JWT"

Additional context
github.com/swaggest/openapi-go v0.2.47
sudorandom/protoc-gen-connect-openapi#5

@vearutop
Copy link
Member

vearutop commented Mar 5, 2024

Thank you for raising this issue, it should be fixed now in v0.2.48.

@sudorandom
Copy link
Author

Awesome! Thanks for fixing this so quickly!

@devleejb
Copy link

devleejb commented Mar 5, 2024

@vearutop
It works well! Thank you for your quick fix!

However, this doesn't work same before.

components:
  securitySchemes:
    basicAuth: # <-- arbitrary name for the security scheme
      type: http
      scheme: basic
security:
  - basicAuth: [] # <-- use the same name here

Reference - Authentication in OpenAPI

@vearutop
Copy link
Member

vearutop commented Mar 5, 2024

@devleejb I'm not sure if I understand, could you explain in more detail the case and your expectations (maybe a reproducer)?

@devleejb
Copy link

devleejb commented Mar 5, 2024

@vearutop
I wrote the test code in openapi31. It is basicAuth test.

func TestSpec_MarshalYAML_3(t *testing.T) {
	var s openapi31.Spec

	spec := `openapi: 3.1.0
info:
  title: MyProject
  description: "My Project Description"
  version: v1.0.0
components:
  securitySchemes:
    basicAuth: # <-- arbitrary name for the security scheme
      type: http
      scheme: basic
security:
  - basicAuth: [] # <-- use the same name here  
paths:
`

	require.NoError(t, s.UnmarshalYAML([]byte(spec)))
}

@vearutop
Copy link
Member

vearutop commented Mar 5, 2024

Ah, I see, thanks, let me quickly fix that. 😅

@sudorandom
Copy link
Author

I was also confused at first! Here's the resulting error just for history:

oneOf constraint failed for SecuritySchemeOrReference with 0 valid results: map[Reference:required key missing: $ref SecurityScheme:oneOf constraint failed for SecurityScheme with 2 valid results: map[APIKey:required key missing: name MutualTLS:bad const value for "type" ("mutualTLS" expected, "http" received) Oauth2:required key missing: flows Oidc:required key missing: openIdConnectUrl]]

@vearutop
Copy link
Member

vearutop commented Mar 5, 2024

Please check v0.2.49.

@devleejb
Copy link

devleejb commented Mar 5, 2024

Thank you very much!

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 a pull request may close this issue.

3 participants