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

chore: replaced kin-openapi library with libopenapi #194

Merged
merged 3 commits into from
Aug 22, 2024

Conversation

Prashansa-K
Copy link
Contributor

deck file openapi2kong command uses kin-openapi
for all its OpenAPI requirements. This library
does not support OpenAPI 3.1, which is a
requirement from the users.
Thus, this change updates the library to libopenapi which can help us to adopt OpenAPI 3.1

For APIOps #1324
Kong/deck#1324


I didn't add much code refactoring in this PR for the aid of the review. I will take that up in a different PR.

@codecov-commenter
Copy link

codecov-commenter commented Aug 14, 2024

Codecov Report

Attention: Patch coverage is 75.66667% with 73 lines in your changes missing coverage. Please review.

Project coverage is 68.19%. Comparing base (e464df7) to head (d14c3ed).

Files Patch % Lines
openapi2kong/openapi2kong.go 68.78% 27 Missing and 27 partials ⚠️
openapi2kong/utils.go 82.75% 8 Missing and 2 partials ⚠️
openapi2kong/jsonschema.go 78.12% 3 Missing and 4 partials ⚠️
openapi2kong/validator.go 92.85% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #194      +/-   ##
==========================================
+ Coverage   67.86%   68.19%   +0.33%     
==========================================
  Files          23       24       +1     
  Lines        2505     2550      +45     
==========================================
+ Hits         1700     1739      +39     
+ Misses        644      643       -1     
- Partials      161      168       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

deck file openapi2kong command uses kin-openapi
for all its OpenAPI requirements. This library
does not support OpenAPI 3.1, which is a
requirement from the users.
Thus, this change updates the library to libopenapi
which can help us to adopt OpenAPI 3.1

For APIOps #1324
Kong/deck#1324
@@ -157,6 +157,7 @@ func (patch *DeckPatch) ApplyToNodes(yamlData *yaml.Node) (err error) {
logbasics.Info("Patch has no selectors specified")
}

//nolint:gosimple
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to my diff.
Thus, I have added a nolint rule here.

@@ -46,6 +46,7 @@ func NewSelectorSet(selectors []string) (SelectorSet, error) {

// IsEmpty returns true if the selector set is empty.
func (set *SelectorSet) IsEmpty() bool {
//nolint:gosimple
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to my diff.
Thus, I have added a nolint rule here.
Same for L71

@hbagdi hbagdi requested a review from GGabriele August 16, 2024 09:48
Copy link

@aboudreault aboudreault left a comment

Choose a reason for hiding this comment

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

nice work @Prashansa-K !! Added some minor comments. Note that I don't know this go-apiops library so I think this work seems safe if the test suite and coverage are good.

@@ -15,6 +15,7 @@ func ApplyNamespaceHost(
deckfile *yaml.Node, // the deckFile to operate on
selectors yamlbasics.SelectorSet, // the selectors to use to select the routes
hosts []string, // the hosts to add to the routes
//nolint:predeclared

Choose a reason for hiding this comment

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

CI doesn't seem to complain, but locally I am getting this error:

namespace/namespace_host.go:18:2: directive `//nolint:predeclared` is unused for linter "predeclared" (nolintlint)
        //nolint:predeclared
``

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It could be because of the differences in the golangci-lint version we are using in the CI and the one used locally.
Let me check with same versions, because I added all the //nolint comments after checking the CI. I wasn't getting any warnings in my local setup.

Comment on lines 28 to 38
for _, schema := range s.AllOf {
dereferenceSchema(schema, seenBefore)
}

for _, schema := range s.AnyOf {
dereferenceSchema(schema, seenBefore)
}

for _, schema := range s.OneOf {
dereferenceSchema(schema, seenBefore)
}

Choose a reason for hiding this comment

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

nit: could probably also be a single for as before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The types had changed in the two underlying libraries.
In kin-openapi (older lib), s.AllOf and others were of type SchemaProxy
In the new one, all these are of type []*SchemaProxy
So, I had to use different for loops. Though, I have changed them to a nested one now. Makes things more readable.

finalSchema := make(map[string]interface{})

if s.IsReference() {
// Add ref key and string

Choose a reason for hiding this comment

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

nit: I would avoid comments that don't add more useful details than what we can read from the code itself.

default:
return nil, fmt.Errorf("expected 'x-kong-tags' to be an array of strings")
kongTags, ok := doc.Extensions.Get("x-kong-tags")

Choose a reason for hiding this comment

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

Suggested change

for i := 0; i < len(tagsArray); i++ {
switch tag := tagsArray[i].(type) {
resultArray := make([]string, len(kongTags.Content))

Choose a reason for hiding this comment

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

Suggested change

}

var jsonBlob interface{}
_ = yaml.Unmarshal(xKongObjectBytes, &jsonBlob)

Choose a reason for hiding this comment

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

any particular reason to not handle the error here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is because we error out in the upcoming line where we check it the jsonBlob can be safely converted to an object of type map[string]interface{}. If not, we are returning a user-specific error.

default:
return nil, fmt.Errorf("expected '/components/x-kong' to be a JSON object")
}
if !ok || xKongComponents == nil {

Choose a reason for hiding this comment

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

same question about the !ok handling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is similar to the above !ok handling.
A component may not have any extensions added via x-kong
Here's an example from this repo itself: https://github.com/Kong/go-apiops/blob/main/docs/mock-a-rena-oas.yml
This spec doesn't use any extensions (top level or component level), but it is still a valid spec and we can generate its equivalent kong config.

}

var xKong interface{}
_ = yaml.Unmarshal(xKongComponentsBytes, &xKong)

Choose a reason for hiding this comment

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

same question about the error handling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same reason as above error handling comment. I have changed this one's handling to how we handle things above as well.

var pluginConfig map[string]interface{}
err = json.Unmarshal(jsonstr, &pluginConfig)
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("failed to parse JSON object for '%s': %%w", extensionName), err)

Choose a reason for hiding this comment

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

Suggested change
return nil, fmt.Errorf(fmt.Sprintf("failed to parse JSON object for '%s': %%w", extensionName), err)
return nil, fmt.Errorf("failed to parse JSON object for '%s': %w", extensionName, err)

kongComponents, kongTags, opts.SkipID)
if err != nil {
return nil, fmt.Errorf("failed to create plugins list from document root: %w", err)
}

// get the OIDC stuff from top level, bail out if the requirements are unsupported
// // get the OIDC stuff from top level, bail out if the requirements are unsupported

Choose a reason for hiding this comment

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

Suggested change
// // get the OIDC stuff from top level, bail out if the requirements are unsupported
// get the OIDC stuff from top level, bail out if the requirements are unsupported

@Prashansa-K
Copy link
Contributor Author

nice work @Prashansa-K !! Added some minor comments. Note that I don't know this go-apiops library so I think this work seems safe if the test suite and coverage are good.

Thank you! I will also test this out with some more examples to be thorough.

@Prashansa-K Prashansa-K merged commit 66febca into main Aug 22, 2024
3 checks passed
@Prashansa-K Prashansa-K deleted the chore/move-to-libopenapi branch August 22, 2024 06:07
Prashansa-K added a commit to Kong/deck that referenced this pull request Aug 22, 2024
This version of go-apiops has replaced kin-openapi
with libopenapi to add further support of
OpenAPI 3.1
Refer: Kong/go-apiops#194
This change would not affect users using OpenAPI 3.0
spec with deck command.

Fix for #1324: #1324
Prashansa-K added a commit to Kong/deck that referenced this pull request Aug 22, 2024
This version of go-apiops has replaced kin-openapi
with libopenapi to add further support of
OpenAPI 3.1
Refer: Kong/go-apiops#194
This change would not affect users using OpenAPI 3.0
spec with deck command.

Fix for #1324: #1324
Prashansa-K added a commit to Kong/deck that referenced this pull request Aug 22, 2024
This version of go-apiops has replaced kin-openapi
with libopenapi to add further support of
OpenAPI 3.1
Refer: Kong/go-apiops#194
This change would not affect users using OpenAPI 3.0
spec with deck command.

Fix for #1324: #1324
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.

4 participants