Skip to content

Commit

Permalink
inline ref when its not component
Browse files Browse the repository at this point in the history
  • Loading branch information
rucciva committed Jun 19, 2024
1 parent 8c418a2 commit 720c9ad
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 11 deletions.
16 changes: 13 additions & 3 deletions internal/bundle/testdata/profile/profile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ paths:
$ref: paths/profiles.yml
/tenants/{tenant-id}/profiles/{profile-id}:
$ref: paths/profile.yml

components:
x-test: {}
/tenants/{tenant-id}:
get:
operationId: "GetTenant"
summary: "Get Tenant"
parameters:
- $ref: tenants.yml#/paths/~1tenants~1{tenant-id}/parameters/0
responses:
200:
$ref: tenants.yml#/paths/~1tenants~1{tenant-id}/get/responses/200
404:
$ref: tenants.yml#/paths/~1tenants~1{tenant-id}/get/responses/404
500:
$ref: tenants.yml#/paths/~1tenants~1{tenant-id}/get/responses/500
77 changes: 77 additions & 0 deletions internal/bundle/testdata/profile/tenants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
openapi: "3.0.0"
info:
title: "Profile API"
version: "1.0.0"
license:
name: "Internal"
url: "http://localhost"
servers:
- url: "https://profile:8443"
- url: "https://localhost:8443"
security:
- {}
paths:
/tenants/{tenant-id}:
parameters:
- name: tenant-id
required: true
in: path
schema:
$ref: "#/components/schemas/TheUUID"
get:
security:
- {}
summary: "get tenant"
operationId: "GetTenant"
responses:
"200":
$ref: "#/components/responses/Tenant"
"404":
$ref: "#/components/responses/TenantNotFound"
"500":
description: "Error"
content:
application/json:
schema:
properties:
message:
$ref: "#/components/schemas/TheZeroableString"
components:
schemas:
TheUUID:
type: string
format: uuid
x-go-type-skip-optional-pointer: true
Tenant:
properties:
id:
$ref: "#/components/schemas/TheUUID"
name:
$ref: "#/components/schemas/TheZeroableString"
TheZeroableString:
type: string
x-go-type-skip-optional-pointer: true
responses:
Error:
description: "error"
content:
"application/json":
schema:
properties:
id:
$ref: "#/components/schemas/TheUUID"
Tenant:
description: "success"
headers:
TraceID:
$ref: "#/components/headers/TraceID"
content:
"application/json":
schema:
$ref: "#/components/schemas/Tenant"
TenantNotFound:
$ref: "#/components/responses/Error"
headers:
TraceID:
schema:
$ref: "#/components/schemas/TheZeroableString"
52 changes: 49 additions & 3 deletions internal/bundle/testoutput/profile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ paths:
description: bad request
"500":
description: server error
/tenants/{tenant-id}:
get:
operationId: "GetTenant"
summary: "Get Tenant"
parameters:
- name: tenant-id
required: true
in: path
schema:
$ref: '#/components/schemas/TheUUID'
responses:
"200":
$ref: '#/components/responses/Tenant'
"404":
$ref: '#/components/responses/TenantNotFound'
"500":
description: "Error"
content:
application/json:
schema:
properties:
message:
$ref: '#/components/schemas/TheZeroableString'
components:
schemas:
UUID:
Expand Down Expand Up @@ -135,6 +158,19 @@ components:
$ref: '#/components/schemas/ZeroableString'
dob:
$ref: '#/components/schemas/ZeroableTime'
TheUUID:
type: string
format: uuid
x-go-type-skip-optional-pointer: true
TheZeroableString:
type: string
x-go-type-skip-optional-pointer: true
Tenant:
properties:
id:
$ref: '#/components/schemas/TheUUID'
name:
$ref: '#/components/schemas/TheZeroableString'
responses:
Error:
description: "error"
Expand All @@ -143,7 +179,7 @@ components:
schema:
properties:
id:
$ref: '#/components/schemas/UUID'
$ref: '#/components/schemas/TheUUID'
Profile:
description: "success"
headers:
Expand All @@ -155,6 +191,17 @@ components:
$ref: '#/components/schemas/Profile'
ProfileNotFound:
$ref: '#/components/responses/Error'
Tenant:
description: "success"
headers:
TraceID:
$ref: '#/components/headers/TraceID'
content:
"application/json":
schema:
$ref: '#/components/schemas/Tenant'
TenantNotFound:
$ref: '#/components/responses/Error'
parameters:
ProfileID:
name: profile-id
Expand All @@ -172,5 +219,4 @@ components:
headers:
TraceID:
schema:
$ref: '#/components/schemas/ZeroableString'
x-test: {}
$ref: '#/components/schemas/TheZeroableString'
19 changes: 14 additions & 5 deletions internal/util/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c Components) copyComponents(docv3 *libopenapi.DocumentModel[v3.Document],
continue
}

err := c.copyComponentNode(ref, prefix)
exist, err := c.copyComponentNode(ref, prefix)
if err != nil {
return fmt.Errorf("fail to locate component: %w", err)
}
Expand All @@ -74,7 +74,12 @@ func (c Components) copyComponents(docv3 *libopenapi.DocumentModel[v3.Document],
continue
}

LocalizeReference(ref, prefix)
if !exist {
ref.Node.Content = idx.GetMappedReferences()[ref.FullDefinition].Node.Content
} else {
LocalizeReference(ref, prefix)
}

}
}

Expand All @@ -91,12 +96,13 @@ func (c Components) copyComponents(docv3 *libopenapi.DocumentModel[v3.Document],
return c.replaceRootNodes(docv3)
}

func (c Components) copyComponentNode(src *index.Reference, prefix string) (err error) {
func (c Components) copyComponentNode(src *index.Reference, prefix string) (exist bool, err error) {
node, err := locateNode(src)
if err != nil {
return fmt.Errorf("fail to locate component: %w", err)
return false, fmt.Errorf("fail to locate component: %w", err)
}

exist = true
name := prefix + src.Name
switch {
case strings.HasPrefix(src.Definition, "#/components/schemas/"):
Expand Down Expand Up @@ -125,9 +131,12 @@ func (c Components) copyComponentNode(src *index.Reference, prefix string) (err

case strings.HasPrefix(src.Definition, "#/components/callbacks/"):
c.Callbacks.Set(name, node)

default:
exist = false
}

return nil
return
}

func locateNode(ref *index.Reference) (node *yaml.Node, err error) {
Expand Down

0 comments on commit 720c9ad

Please sign in to comment.