Skip to content

Commit

Permalink
add support for alias node
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim committed Aug 16, 2022
1 parent 622d9a7 commit 3fa115d
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func yamlNode(root *yaml.Node) (interface{}, error) {
case yaml.ScalarNode:
return yamlScalar(root)
case yaml.AliasNode:
return nil, fmt.Errorf("no translation to JSON for AliasNode")
return yamlNode(root.Alias)
default:
return nil, fmt.Errorf("unsupported YAML node type: %v", root.Kind)
}
Expand Down
187 changes: 187 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ tag:
ny, err := ndata.MarshalYAML()
require.NoError(t, err)
assert.Equal(t, nestpected, string(ny.([]byte)))

ydoc, err := BytesToYAMLDoc([]byte(fixtures2224))
require.NoError(t, err)
b, err := YAMLToJSON(ydoc)
require.NoError(t, err)

var bdata JSONMapSlice
require.NoError(t, json.Unmarshal(b, &bdata))

}

func TestYAMLToJSON(t *testing.T) {
Expand Down Expand Up @@ -209,6 +218,184 @@ func TestMapKeyTypes(t *testing.T) {
assert.NoError(t, err)
}

const fixtures2224 = `definitions:
Time:
type: string
format: date-time
x-go-type:
import:
package: time
embedded: true
type: Time
x-nullable: true
TimeAsObject: # <- time.Time is actually a struct
type: string
format: date-time
x-go-type:
import:
package: time
hints:
kind: object
embedded: true
type: Time
x-nullable: true
Raw:
x-go-type:
import:
package: encoding/json
hints:
kind: primitive
embedded: true
type: RawMessage
Request:
x-go-type:
import:
package: net/http
hints:
kind: object
embedded: true
type: Request
RequestPointer:
x-go-type:
import:
package: net/http
hints:
kind: object
nullable: true
embedded: true
type: Request
OldStyleImport:
type: object
x-go-type:
import:
package: net/http
type: Request
hints:
noValidation: true
OldStyleRenamed:
type: object
x-go-type:
import:
package: net/http
type: Request
hints:
noValidation: true
x-go-name: OldRenamed
ObjectWithEmbedded:
type: object
properties:
a:
$ref: '#/definitions/Time'
b:
$ref: '#/definitions/Request'
c:
$ref: '#/definitions/TimeAsObject'
d:
$ref: '#/definitions/Raw'
e:
$ref: '#/definitions/JSONObject'
f:
$ref: '#/definitions/JSONMessage'
g:
$ref: '#/definitions/JSONObjectWithAlias'
ObjectWithExternals:
type: object
properties:
a:
$ref: '#/definitions/OldStyleImport'
b:
$ref: '#/definitions/OldStyleRenamed'
Base:
properties: &base
id:
type: integer
format: uint64
x-go-custom-tag: 'gorm:"primary_key"'
FBID:
type: integer
format: uint64
x-go-custom-tag: 'gorm:"index"'
created_at:
$ref: "#/definitions/Time"
updated_at:
$ref: "#/definitions/Time"
version:
type: integer
format: uint64
HotspotType:
type: string
enum:
- A
- B
- C
Hotspot:
type: object
allOf:
- properties: *base
- properties:
access_points:
type: array
items:
$ref: '#/definitions/AccessPoint'
type:
$ref: '#/definitions/HotspotType'
required:
- type
AccessPoint:
type: object
allOf:
- properties: *base
- properties:
mac_address:
type: string
x-go-custom-tag: 'gorm:"index;not null;unique"'
hotspot_id:
type: integer
format: uint64
hotspot:
$ref: '#/definitions/Hotspot'
JSONObject:
type: object
additionalProperties:
type: array
items:
$ref: '#/definitions/Raw'
JSONObjectWithAlias:
type: object
additionalProperties:
type: object
properties:
message:
$ref: '#/definitions/JSONMessage'
JSONMessage:
$ref: '#/definitions/Raw'
Incorrect:
x-go-type:
import:
package: net
hints:
kind: array
embedded: true
type: Buffers
x-nullable: true
`

const withQuotedYKey = `consumes:
- application/json
definitions:
Expand Down

0 comments on commit 3fa115d

Please sign in to comment.