Skip to content

Sort security scheme provider names#311

Merged
deepmap-marcinr merged 1 commit intooapi-codegen:masterfrom
alicecarlotti:sort-provider-names
Mar 18, 2021
Merged

Sort security scheme provider names#311
deepmap-marcinr merged 1 commit intooapi-codegen:masterfrom
alicecarlotti:sort-provider-names

Conversation

@alicecarlotti
Copy link
Copy Markdown
Contributor

This fixes #310 by ensuring that the generated constants are output in a deterministic (alphabetical) order.

This ensures the generated constants are output in a deterministic order.
@alicecarlotti
Copy link
Copy Markdown
Contributor Author

I think that unsorted SecurityDefinitions is the root cause. Some templates references SecurityDefinitions, so these also have the order problem.

SecurityDefinitions is a slice, so the order of iteration will be deterministic if elements are added in a deterministic order (which I'd guess is already happening, though I haven't verified this).

providerNameMap is generated from SecurityDefinitions. Just sorting the return value of DescribeSecurityDefinition probably solves these problems.

Sorting SecurityDefinitions (if it isn't already deterministically ordered) would not solve the problem with providerNameMap, since the order of iteration over a map is explicitly randomized by Go. We use the map for efficient deduplication, so we either need to sort the resulting contents (as is done in this PR), or use a different data structure for deduplication.

@neglect-yp
Copy link
Copy Markdown
Contributor

Sorry, I misunderstood because these are similar problems.

Sorting SecurityDefinitions (if it isn't already deterministically ordered) would not solve the problem with providerNameMap

Yes, I was wrong. However, SecurityDefinitions is not deterministically ordered. I'll create another issue and PR.

I've checked this problem on this PR branch.

test.yaml

openapi: 3.0.2
paths:
  /a:
    get:
      oparationId: a
      security:
        - ApiId: []
          ApiKey: []

components:
  ApiId:
    type: apiKey
    in: header
    name: X-Api-Id
  ApiKey:
    type: apiKey
    in: header
    name: X-Api-Key
$ diff -u <(go run ./cmd/oapi-codegen/ test.yaml) <(go run ./cmd/oapi-codegen/ test.yaml)
--- /dev/fd/11	2021-03-17 13:02:42.000000000 +0900
+++ /dev/fd/12	2021-03-17 13:02:42.000000000 +0900
@@ -251,10 +251,10 @@
 func (w *ServerInterfaceWrapper) GetA(ctx echo.Context) error {
 	var err error

-	ctx.Set(ApiKeyScopes, []string{""})
-
 	ctx.Set(ApiIdScopes, []string{""})

+	ctx.Set(ApiKeyScopes, []string{""})
+
 	// Invoke the callback with all the unmarshalled arguments
 	err = w.Handler.GetA(ctx)
 	return err

@deepmap-marcinr deepmap-marcinr merged commit d062bd3 into oapi-codegen:master Mar 18, 2021
adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
This ensures the generated constants are output in a deterministic order.
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.

Non-deterministic ordering security scheme provider constants

3 participants