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

Escape in $ref #240

Closed
ShouheiNishi opened this issue Jan 25, 2024 · 2 comments
Closed

Escape in $ref #240

ShouheiNishi opened this issue Jan 25, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ShouheiNishi
Copy link

It seems that escaping is not handled in the processing of the JSON pointer of the $ref.

issue1.yaml

openapi: 3.0.2
info:
  title: "issue1"
  version: "0.0.1"
paths:
  /foo/bar:
    $ref: 'issue2.yaml#/paths/~1foo~1bar'

issue2.yaml

openapi: 3.0.2
info:
  title: "issue2"
  version: "0.0.1"
paths:
  /foo/bar:
    get:
      operationId: Test
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object
                properties:
                  test1:
                    type: string

loader_test.go

package work_test

import (
        "os"
        "testing"

        "github.com/pb33f/libopenapi"
        "github.com/pb33f/libopenapi/datamodel"
        "github.com/stretchr/testify/assert"
)

func TestLoader(t *testing.T) {
        data, err := os.ReadFile("issue1.yaml")
        assert.NoError(t, err)

        document, err := libopenapi.NewDocumentWithConfiguration(data, &datamodel.DocumentConfiguration{
                AllowFileReferences: true,
        })
        assert.NoError(t, err)

        _, errs := document.BuildV3Model()
        assert.Empty(t, errs)
}

go.mod

module issue

go 1.21.0

require (
        github.com/pb33f/libopenapi v0.15.1
        github.com/stretchr/testify v1.8.4
)

require (
        github.com/bahlo/generic-list-go v0.2.0 // indirect
        github.com/buger/jsonparser v1.1.1 // indirect
        github.com/davecgh/go-spew v1.1.1 // indirect
        github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
        github.com/mailru/easyjson v0.7.7 // indirect
        github.com/pmezard/go-difflib v1.0.0 // indirect
        github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
        github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
        golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb // indirect
        golang.org/x/sync v0.1.0 // indirect
        gopkg.in/yaml.v3 v3.0.1 // indirect
)

Error result is here.

$ go test ./...
--- FAIL: TestLoader (0.00s)
    loader_test.go:22:
                Error Trace:    /home/nishi/free5gc-deb/free5gc/work/loader_test.go:22
                Error:          Should be empty, but was [component '#/paths/~1foo~1bar' does not exist in the specification]
                Test:           TestLoader
FAIL
FAIL    issue   0.012s
FAIL
$

https://github.com/OpenAPITools/openapi-generator can process it.

$ docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate -i /local/issue1.yaml -g go
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: go (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'go' is considered stable.
[main] INFO  o.o.c.languages.AbstractGoCodegen - Environment variable GO_POST_PROCESS_FILE not defined so Go code may not be properly formatted. To define it, try `export GO_POST_PROCESS_FILE="/usr/local/bin/gofmt -w"` (Linux/Mac)
[main] INFO  o.o.c.languages.AbstractGoCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.codegen.InlineModelResolver - Inline schema created as Test_200_response. To have complete control of the model name, set the `title` field or use the modelNameMapping option (e.g. --model-name-mappings Test_200_response=NewModel,ModelA=NewModelA in CLI) or inlineSchemaNameMapping option (--inline-schema-name-mappings Test_200_response=NewModel,ModelA=NewModelA in CLI).
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.TemplateManager - writing file ./model_test_200_response.go
[main] INFO  o.o.codegen.TemplateManager - writing file ./docs/Test200Response.md
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.TemplateManager - writing file ./api_default.go
[main] INFO  o.o.codegen.TemplateManager - writing file ./test/api_default_test.go
[main] INFO  o.o.codegen.TemplateManager - writing file ./docs/DefaultAPI.md
[main] INFO  o.o.codegen.utils.URLPathUtils - 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] INFO  o.o.codegen.TemplateManager - writing file ./api/openapi.yaml
[main] INFO  o.o.codegen.TemplateManager - writing file ./README.md
[main] INFO  o.o.codegen.TemplateManager - writing file ./git_push.sh
[main] INFO  o.o.codegen.TemplateManager - writing file ./.gitignore
[main] INFO  o.o.codegen.TemplateManager - writing file ./configuration.go
[main] INFO  o.o.codegen.TemplateManager - writing file ./client.go
[main] INFO  o.o.codegen.TemplateManager - writing file ./response.go
[main] INFO  o.o.codegen.TemplateManager - writing file ./go.mod
[main] INFO  o.o.codegen.TemplateManager - writing file ./go.sum
[main] INFO  o.o.codegen.TemplateManager - writing file /./.travis.yml
[main] INFO  o.o.codegen.TemplateManager - writing file ./utils.go
[main] INFO  o.o.codegen.TemplateManager - writing file /./.openapi-generator-ignore
[main] INFO  o.o.codegen.TemplateManager - writing file ./.openapi-generator/VERSION
[main] INFO  o.o.codegen.TemplateManager - writing file ./.openapi-generator/FILES
################################################################################
# Thanks for using OpenAPI Generator.                                          #
# Please consider donation to help us maintain this project ?                 #
# https://opencollective.com/openapi_generator/donate                          #
################################################################################
$
@daveshanley
Copy link
Member

oh dear, this is a bug.

@daveshanley daveshanley added the bug Something isn't working label Jan 25, 2024
daveshanley added a commit that referenced this issue Feb 3, 2024
Not sure why I built the original code the way it was, but it’s now a simplified query without stripping out, or manipulating the path in anyway, the rest of the code that follows after handles the query correctly.

Signed-off-by: quobix <dave@quobix.com>
@daveshanley daveshanley mentioned this issue Feb 3, 2024
daveshanley added a commit that referenced this issue Feb 3, 2024
Not sure why I built the original code the way it was, but it’s now a simplified query without stripping out, or manipulating the path in anyway, the rest of the code that follows after handles the query correctly.

Signed-off-by: quobix <dave@quobix.com>
@daveshanley
Copy link
Member

Resolved in v0.15.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants