Skip to content

Commit 9f7c838

Browse files
JounQinCopilot
andauthored
fix: $ref compatibility (#216)
Co-authored-by: Copilot <[email protected]>
1 parent 2c78f79 commit 9f7c838

File tree

7 files changed

+90
-28
lines changed

7 files changed

+90
-28
lines changed

.changeset/shaggy-bees-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@alauda/doom": patch
3+
---
4+
5+
fix: $ref compatibility

docs/zh/apis/advanced-apis/workload/daemonset.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
path="/apis/apps/v1/daemonsets"
55
pathPrefix="/kubernetes/{cluster}"
66
/>
7+
8+
<OpenAPIPath
9+
path="/apis/apps/v1/namespaces/{namespace}/daemonsets/{name}"
10+
pathPrefix="/kubernetes/{cluster}"
11+
/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DaemonSet
2+
3+
<OpenAPIRef schema="io.k8s.api.apps.v1.DaemonSet" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Deployment
2+
3+
<OpenAPIRef schema="io.k8s.api.apps.v1.Deployment" />

doom.config.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ api:
1313
openapis:
1414
- docs/shared/openapis/*.json
1515
references:
16-
v1alpha1.CodeQualityBranch: /apis/references/CodeQualityBranch#CodeQualityBranch
16+
v1alpha1.CodeQualityBranch: /apis/references/CodeQualityBranch#v1alpha1.CodeQualityBranch
17+
io.k8s.api.apps.v1.DaemonSet: /apis/references/DaemonSet#io.k8s.api.apps.v1.DaemonSet
18+
io.k8s.api.apps.v1.Deployment: /apis/references/Deployment#io.k8s.api.apps.v1.Deployment
19+
io.k8s.api.core.v1.ObjectFieldSelector: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/object-field-selector/
20+
io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/delete-options/
21+
io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/label-selector/
22+
io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/list-meta/
23+
io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/object-meta/
24+
io.k8s.apimachinery.pkg.apis.meta.v1.Patch: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/patch/
25+
io.k8s.apimachinery.pkg.apis.meta.v1.Status: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/status/
1726
permission:
1827
functionresources:
1928
- docs/shared/functionresources/*.yaml

packages/doom/src/runtime/components/OpenAPIRef.tsx

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ export interface OpenAPIRefProps {
3434
collectRefs?: boolean
3535
}
3636

37+
export const get$Ref = (
38+
obj: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject,
39+
) => {
40+
if ('$ref' in obj && obj.$ref) {
41+
return obj.$ref
42+
}
43+
if ('allOf' in obj && Array.isArray(obj.allOf)) {
44+
for (const item of obj.allOf) {
45+
if ('$ref' in item && item.$ref) {
46+
return item.$ref
47+
}
48+
}
49+
}
50+
}
51+
3752
export const OpenAPIProperty = ({
3853
name,
3954
property,
@@ -43,53 +58,74 @@ export const OpenAPIProperty = ({
4358
property: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.SchemaObject
4459
openapi: OpenAPIV3_1.Document
4560
}) => {
46-
const propObj =
47-
'$ref' in property ? resolveRef(openapi, property.$ref) : property
61+
const prop$Ref = get$Ref(property)
62+
63+
const propObj = prop$Ref
64+
? resolveRef(openapi, prop$Ref)
65+
: (property as OpenAPIV3_1.SchemaObject)
66+
4867
const type = propObj.type
68+
4969
let typeNode: ReactNode
5070
let extraNode: ReactNode
71+
5172
if (type === 'array') {
5273
const { items } = propObj
53-
const itemsObj = '$ref' in items ? resolveRef(openapi, items.$ref) : items
74+
const items$Ref = get$Ref(items)
75+
const itemsObj = items$Ref
76+
? resolveRef(openapi, items$Ref)
77+
: (items as OpenAPIV3_1.SchemaObject)
5478
const itemsType = itemsObj.type
5579
typeNode = (
5680
<code>
5781
[]
58-
{'$ref' in items ? <RefLink $ref={items.$ref} /> : itemsType}
82+
{items$Ref ? <RefLink $ref={items$Ref} /> : itemsType}
5983
</code>
6084
)
6185
} else if (type === 'object') {
62-
if ('properties' in property && property.properties) {
63-
extraNode = (
64-
<div className="my-4">
65-
<em>Properties:</em>
66-
<OpenAPIProperties
67-
properties={property.properties}
68-
openapi={openapi}
69-
/>
70-
</div>
71-
)
86+
if (prop$Ref) {
87+
typeNode = <RefLink $ref={prop$Ref} />
7288
}
89+
7390
if (typeof propObj.additionalProperties === 'object') {
7491
const props = propObj.additionalProperties
75-
const propsObj = '$ref' in props ? resolveRef(openapi, props.$ref) : props
92+
const props$Ref = get$Ref(props)
93+
const propsObj = props$Ref
94+
? resolveRef(openapi, props$Ref)
95+
: (props as OpenAPIV3_1.SchemaObject)
7696
const propsType = propsObj.type
7797
typeNode = (
78-
<code>
79-
map[string]
80-
{typeof propsType === 'string'
81-
? propsType
82-
: '$ref' in props && <RefLink $ref={props.$ref} />}
83-
</code>
98+
<>
99+
{typeNode}
100+
<code>
101+
map[string]
102+
{typeof propsType === 'string'
103+
? propsType
104+
: props$Ref && <RefLink $ref={props$Ref} />}
105+
</code>
106+
</>
84107
)
85-
} else {
108+
109+
if ('properties' in property && property.properties) {
110+
extraNode = (
111+
<div className="my-4">
112+
<em>Properties:</em>
113+
<OpenAPIProperties
114+
properties={property.properties}
115+
openapi={openapi}
116+
/>
117+
</div>
118+
)
119+
}
120+
} else if (!prop$Ref) {
86121
typeNode = <code>{type}</code>
87122
}
88123
} else if (typeof type === 'string') {
89124
typeNode = <code>{type}</code>
90-
} else if ('$ref' in property) {
91-
typeNode = <RefLink $ref={property.$ref} />
125+
} else if (prop$Ref) {
126+
typeNode = <RefLink $ref={prop$Ref} />
92127
}
128+
93129
return (
94130
<>
95131
{name && (

packages/doom/src/runtime/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ export const resolveRef = <
2222
}
2323

2424
const DEFAULT_COMMON_REFS = {
25-
'v1alpha1.ListMeta': 'common-definitions/list-meta/#ListMeta',
26-
'v1.ObjectMeta': 'common-definitions/object-meta/#ObjectMeta',
25+
'v1alpha1.ListMeta': 'list-meta/',
26+
'v1.ObjectMeta': 'object-meta/',
2727
}
2828

29-
const K8S_DOC_PREFIX = 'https://kubernetes.io/docs/reference/kubernetes-api/'
29+
const K8S_DOC_PREFIX =
30+
'https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/'
3031

3132
export const COMMON_REFS = {
3233
...Object.fromEntries(

0 commit comments

Comments
 (0)