Skip to content

Commit

Permalink
codescan: Natively support json.RawMessage
Browse files Browse the repository at this point in the history
Before this patch, `encoding/json.RawMessage` would be interpreted as []uint8, which `encoding/json` is incapable of decoding. This patch addresses that by checking if a type's name is `encoding/json.RawMessage` and if so, sets `type: object`.

In #1622 it was suggested to use `type: string` with `format: binary` which is however not the correct use, because this is not an serialized JSON object but a deserialized JSON object.

If my understanding is correct, `type: string` with `fromat: binary` would expect something like:

```
{ "foo": "[bar]" }
```

for

```
type f struct { Foo json.RawMessage `json:"foo"` }
```

whereas `json.RawMessage` actually implies:

```
{ "foo": [bar] }
```

I've tried to mess around with `x-go-type`

```go
			tgt.AddExtension("x-go-type", map[string]interface{}{
				"import": map[string]interface{}{
					"alias":   "json",
					"package": "encoding/json",
				},
				"type": "RawMessage",
			})
```

but that yielded no result when generating the client code but I believe it to be the right extension here, it does seem however that its applicability is limited as discussed in: #1879 (comment)

I do understand that `json.RawMessage` can be any defined JSON type, and that this is just a workaround for now, but it's better than what we have so far. I think a `oneOf` would be the appropriate setting here but I'm not sure how to implement that in this particular location. Happy for any pointers though.

Closes #1622

Related #1879

Signed-off-by: aeneasr <aeneas@ory.sh>
  • Loading branch information
aeneasr committed Nov 13, 2019
1 parent 5343050 commit 2f7f95e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions codescan/schema.go
Expand Up @@ -263,6 +263,10 @@ func (s *schemaBuilder) buildFromType(tpe types.Type, tgt swaggerTypable) error
tgt.Typed("string", "date-time")
return nil
}
if pkg.PkgPath == "encoding/json" && tio.Name() == "RawMessage" {
tgt.Typed("object", "")
return nil
}
cmt, hasComments := s.ctx.FindComments(pkg, tio.Name())
if !hasComments {
cmt = new(ast.CommentGroup)
Expand Down

0 comments on commit 2f7f95e

Please sign in to comment.