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

InternalizeRefs does not internalize parameters of paths #959

Closed
selaux opened this issue Jun 5, 2024 · 0 comments · Fixed by #960
Closed

InternalizeRefs does not internalize parameters of paths #959

selaux opened this issue Jun 5, 2024 · 0 comments · Fixed by #960

Comments

@selaux
Copy link
Contributor

selaux commented Jun 5, 2024

Calling .InternalizeRefs on a schema that contains parameters on the path level does not seem to resolve those parameters correctly. The resulting schema still contains the reference to the external file. When moving the parameters below the operation level, the behavior is as expected.

Schema
openapi: 3.0.0
info:
  title: foo
  version: 0.0.0
paths:
  /{external1}:
    parameters:
      - in: path
        name: external1
        required: true
        schema:
          $ref: './components.yaml#/components/schemas/External1'
    get:
      responses:
        '204':
          description: No content
components.yaml
components:
  schemas:
    External1:
      type: string
Code
package main

import (
  "context"
  "encoding/json"
  "log"
  "os"

  "github.com/getkin/kin-openapi/openapi3"
)

func main() {
  loader := openapi3.NewLoader()
  loader.IsExternalRefsAllowed = true
  doc, err := loader.LoadFromFile("openapi.yaml")
  if err != nil {
    log.Fatalf("load: %v", err)
  }

  doc.InternalizeRefs(context.Background(), nil)

  result, err := json.MarshalIndent(doc, "", "  ")
  if err != nil {
    log.Fatalf("marshal: %v", err)
  }

  err = os.WriteFile("result.json", result, 0644)
  if err != nil {
    log.Fatalf("result: %v", err)
  }
}
Expected
{
  "components": {
    "schemas": {
      "External1": {
        "type": "string"
      }
    }
  },
  "info": {
    "title": "foo",
    "version": "0.0.0"
  },
  "openapi": "3.0.0",
  "paths": {
    "/{external1}": {
      "get": {
        "responses": {
          "204": {
            "description": "No content"
          }
        }
      },
      "parameters": [
        {
          "in": "path",
          "name": "external1",
          "required": true,
          "schema": {
            "$ref": "#/components/schemas/External1"
          }
        }
      ]
    }
  }
}
Got
{
  "info": {
    "title": "foo",
    "version": "0.0.0"
  },
  "openapi": "3.0.0",
  "paths": {
    "/{external1}": {
      "get": {
        "responses": {
          "204": {
            "description": "No content"
          }
        }
      },
      "parameters": [
        {
          "in": "path",
          "name": "external1",
          "required": true,
          "schema": {
            "$ref": "./components.yaml#/components/schemas/External1"
          }
        }
      ]
    }
  }
}
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.

1 participant