| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package strict |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/pb33f/libopenapi" |
| 10 |
"github.com/pb33f/libopenapi/datamodel/high/base" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestExtractSchemaLocation_NilSchema(t *testing.T) { |
| 16 |
line, col := extractSchemaLocation(nil) |
| 17 |
assert.Equal(t, 0, line) |
| 18 |
assert.Equal(t, 0, col) |
| 19 |
} |
| 20 |
|
| 21 |
func TestExtractSchemaLocation_WithValidSchema(t *testing.T) { |
| 22 |
spec := `openapi: "3.1.0" |
| 23 |
info: |
| 24 |
title: Test |
| 25 |
version: "1.0" |
| 26 |
paths: |
| 27 |
/test: |
| 28 |
get: |
| 29 |
responses: |
| 30 |
"200": |
| 31 |
description: OK |
| 32 |
content: |
| 33 |
application/json: |
| 34 |
schema: |
| 35 |
type: object |
| 36 |
properties: |
| 37 |
name: |
| 38 |
type: string` |
| 39 |
|
| 40 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 41 |
require.NoError(t, err) |
| 42 |
|
| 43 |
model, errs := doc.BuildV3Model() |
| 44 |
require.Empty(t, errs) |
| 45 |
|
| 46 |
schema := model.Model.Paths.PathItems.GetOrZero("/test").Get.Responses.Codes.GetOrZero("200").Content.GetOrZero("application/json").Schema.Schema() |
| 47 |
require.NotNil(t, schema) |
| 48 |
|
| 49 |
line, col := extractSchemaLocation(schema) |
| 50 |
// The schema starts at line 14 (type: object) |
| 51 |
assert.Greater(t, line, 0, "line should be greater than 0") |
| 52 |
assert.Greater(t, col, 0, "col should be greater than 0") |
| 53 |
} |
| 54 |
|
| 55 |
func TestExtractSchemaLocation_SchemaWithNilGoLow(t *testing.T) { |
| 56 |
// Create a high-level schema programmatically (no GoLow()) |
| 57 |
// This covers types.go:108 where low == nil |
| 58 |
schema := &base.Schema{ |
| 59 |
Type: []string{"object"}, |
| 60 |
} |
| 61 |
|
| 62 |
// GoLow() returns nil for programmatically created schemas |
| 63 |
require.Nil(t, schema.GoLow()) |
| 64 |
|
| 65 |
line, col := extractSchemaLocation(schema) |
| 66 |
assert.Equal(t, 0, line) |
| 67 |
assert.Equal(t, 0, col) |
| 68 |
} |
| 69 |
|
| 70 |
func TestNewUndeclaredProperty_WithLocation(t *testing.T) { |
| 71 |
spec := `openapi: "3.1.0" |
| 72 |
info: |
| 73 |
title: Test |
| 74 |
version: "1.0" |
| 75 |
paths: |
| 76 |
/test: |
| 77 |
post: |
| 78 |
requestBody: |
| 79 |
content: |
| 80 |
application/json: |
| 81 |
schema: |
| 82 |
type: object |
| 83 |
properties: |
| 84 |
name: |
| 85 |
type: string` |
| 86 |
|
| 87 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 88 |
require.NoError(t, err) |
| 89 |
|
| 90 |
model, errs := doc.BuildV3Model() |
| 91 |
require.Empty(t, errs) |
| 92 |
|
| 93 |
schema := model.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema.Schema() |
| 94 |
require.NotNil(t, schema) |
| 95 |
|
| 96 |
undeclared := newUndeclaredProperty( |
| 97 |
"$.body.extra", |
| 98 |
"extra", |
| 99 |
"value", |
| 100 |
[]string{"name"}, |
| 101 |
DirectionRequest, |
| 102 |
schema, |
| 103 |
) |
| 104 |
|
| 105 |
assert.Equal(t, "$.body.extra", undeclared.Path) |
| 106 |
assert.Equal(t, "extra", undeclared.Name) |
| 107 |
assert.Equal(t, "property", undeclared.Type) |
| 108 |
assert.Greater(t, undeclared.SpecLine, 0, "SpecLine should be set") |
| 109 |
assert.Greater(t, undeclared.SpecCol, 0, "SpecCol should be set") |
| 110 |
} |
| 111 |
|
| 112 |
func TestNewUndeclaredProperty_WithNilSchema(t *testing.T) { |
| 113 |
undeclared := newUndeclaredProperty( |
| 114 |
"$.body.extra", |
| 115 |
"extra", |
| 116 |
"value", |
| 117 |
[]string{"name"}, |
| 118 |
DirectionRequest, |
| 119 |
nil, // nil schema |
| 120 |
) |
| 121 |
|
| 122 |
assert.Equal(t, "$.body.extra", undeclared.Path) |
| 123 |
assert.Equal(t, "extra", undeclared.Name) |
| 124 |
assert.Equal(t, "property", undeclared.Type) |
| 125 |
assert.Equal(t, 0, undeclared.SpecLine, "SpecLine should be 0 for nil schema") |
| 126 |
assert.Equal(t, 0, undeclared.SpecCol, "SpecCol should be 0 for nil schema") |
| 127 |
} |