Skip to content

Commit be3b9ef

Browse files
authored
Merge pull request #4 from livesession/chore/x-go-type-override-on-ref
chore(x-go-type-override-on-ref): if x-go-type on allOf then it's mor…
2 parents b191cde + 6879f48 commit be3b9ef

9 files changed

Lines changed: 79 additions & 14 deletions

File tree

internal/test/x-go/x-go-embedding/all_of/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
openapi: "3.0.1"
22
info:
33
version: 1.0.0
4-
title: Tests x-go-embeding AllOf composition
4+
title: Tests x-go-embedding AllOf composition
55
paths:
66
/placeholder:
77
get:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
abc:
2+
go run github.com/livesession/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml openapi.yaml
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# yaml-language-server: $schema=../../configuration-schema.json
2+
package: x_go_type_override_on_ref
3+
output: x_go_type_override_on_ref.gen.go
4+
generate:
5+
models: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package x_go_type_override_on_ref
2+
3+
//go:generate go run github.com/livesession/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml openapi.yaml
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
openapi: "3.0.1"
2+
3+
components:
4+
schemas:
5+
Pagination:
6+
type: object
7+
properties:
8+
page:
9+
type: integer
10+
perPage:
11+
type: integer
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
openapi: "3.0.1"
2+
info:
3+
version: 1.0.0
4+
title: Tests x-go-embedding override on $ref
5+
paths:
6+
/placeholder:
7+
get:
8+
operationId: placeholder
9+
responses:
10+
default:
11+
description: placeholder
12+
content:
13+
application/json:
14+
schema:
15+
$ref: "#/components/schemas/response.Placeholder"
16+
components:
17+
schemas:
18+
response.Placeholder:
19+
x-go-type: Placeholder
20+
allOf:
21+
- $ref: "#/components/schemas/response.Base"
22+
23+
response.Base:
24+
type: object
25+
properties:
26+
id:
27+
type: string
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package x_go_type_override_on_ref
2+
3+
type Placeholder struct {
4+
Name string `json:"name,omitempty"`
5+
}

internal/test/x-go/x-go-type/override_on_ref/x_go_type_override_on_ref.gen.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/codegen/schema.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,6 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
296296
SkipOptionalPointer: skipOptionalPointer,
297297
}
298298

299-
// AllOf is interesting, and useful. It's the union of a number of other
300-
// schemas. A common usage is to create a union of an object with an ID,
301-
// so that in a RESTful paradigm, the Create operation can return
302-
// (object, id), so that other operations can refer to (id)
303-
if schema.AllOf != nil {
304-
mergedSchema, err := MergeSchemas(schema, path)
305-
if err != nil {
306-
return Schema{}, fmt.Errorf("error merging schemas: %w", err)
307-
}
308-
mergedSchema.OAPISchema = schema
309-
return mergedSchema, nil
310-
}
311-
312299
// Check x-go-type, which will completely override the definition of this
313300
// schema with the provided type.
314301
if extension, ok := schema.Extensions[extPropGoType]; ok {
@@ -333,6 +320,19 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
333320
return outSchema, nil
334321
}
335322
}
323+
324+
// AllOf is interesting, and useful. It's the union of a number of other
325+
// schemas. A common usage is to create a union of an object with an ID,
326+
// so that in a RESTful paradigm, the Create operation can return
327+
// (object, id), so that other operations can refer to (id)
328+
if schema.AllOf != nil {
329+
mergedSchema, err := MergeSchemas(schema, path)
330+
if err != nil {
331+
return Schema{}, fmt.Errorf("error merging schemas: %w", err)
332+
}
333+
mergedSchema.OAPISchema = schema
334+
return mergedSchema, nil
335+
}
336336

337337
// Schema type and format, eg. string / binary
338338
t := schema.Type

0 commit comments

Comments
 (0)