Skip to content

test: cover optional security alternatives - #159

Open
kriptoburak wants to merge 1 commit into
swaggest:masterfrom
kriptoburak:codex/optional-security-yaml-test
Open

test: cover optional security alternatives#159
kriptoburak wants to merge 1 commit into
swaggest:masterfrom
kriptoburak:codex/optional-security-yaml-test

Conversation

@kriptoburak

Copy link
Copy Markdown

Summary

  • Adds OpenAPI 3.0 and 3.1 YAML unmarshal coverage for optional security alternatives.
  • Verifies empty security requirements can sit alongside API key security requirements.

Validation

  • go test ./openapi3 ./openapi31 -run OptionalSecurityAlternative

@llamapreview llamapreview Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI Code Review by LlamaPReview

🎯 TL;DR & Recommendation

Recommendation: Approve with suggestions

This PR adds YAML unmarshal test coverage for optional security alternatives in OpenAPI 3.0 and 3.1. While the tests function correctly, two improvements would enhance clarity and robustness.

🌟 Strengths

  • Effective coverage for a scenario previously untested.
  • Consistent with existing test patterns in the codebase.

💡 Suggestions (P2)

  • openapi3/entities_extra_test.go: The test function name TestSpec_MarshalYAML_OptionalSecurityAlternative misleadingly suggests marshalling, but it tests unmarshalling. Renaming would reduce confusion for future maintainers.
  • openapi3/entities_extra_test.go: The test only asserts no error during unmarshalling. Adding explicit assertions on the parsed security fields would catch potential bugs in handling empty security alternatives.

💡 Have feedback? We'd love to hear it in our GitHub Discussions.
✨ This review was generated by LlamaPReview Advanced, which is free for all open-source projects. Learn more.

Comment thread openapi3/entities_extra_test.go Outdated
Comment on lines +76 to +80
func TestSpec_MarshalYAML_OptionalSecurityAlternative(t *testing.T) {
var s openapi3.Spec

spec := `openapi: 3.0.3
info:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 | Confidence: High

The function name starts with TestSpec_MarshalYAML_, which implies the test exercises marshalling (e.g., MarshalYAML). However, the test body calls UnmarshalYAML, not MarshalYAML. This is misleading for developers reading or maintaining the test. While it matches the naming pattern of existing tests in this file (which have the same inconsistency), it perpetuates technical debt that reduces clarity.

Suggested change
func TestSpec_MarshalYAML_OptionalSecurityAlternative(t *testing.T) {
var s openapi3.Spec
spec := `openapi: 3.0.3
info:
func TestSpec_UnmarshalYAML_OptionalSecurityAlternative(t *testing.T) {
var s openapi3.Spec
spec := `openapi: 3.0.3
info:
title: Optional Security API

Comment on lines +99 to +101
require.NoError(t, s.UnmarshalYAML([]byte(spec)))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 | Confidence: Medium

Speculative: The test only asserts that UnmarshalYAML returns no error. It does not validate the content of the parsed Security field. For example, it does not check that s.Security has exactly two elements, the first being an empty map (representing an optional alternative with no security) and the second containing "apiKeyAuth". If the library mishandles empty security objects (e.g., ignores them, stores nil, or merges them), this test would pass despite producing incorrect results. Adding explicit assertions would make the test robust and reveal potential unmarshalling bugs.

Code Suggestion:

require.NoError(t, s.UnmarshalYAML([]byte(spec)))
	require.Len(t, s.Security, 2)
	require.Empty(t, s.Security[0])
	require.Contains(t, s.Security[1], "apiKeyAuth")
}

@kriptoburak
kriptoburak force-pushed the codex/optional-security-yaml-test branch from 1aeb6b0 to 73519c7 Compare July 2, 2026 23:08
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.

1 participant