fix: Remove openAPI docs

This commit is contained in:
Ilya Kuznetsov 2024-12-18 20:53:38 +01:00
parent fe4c6b8a68
commit cfa2be3c35
No known key found for this signature in database
GPG Key ID: 91F3DDCF5D21CDDF
3 changed files with 30 additions and 4634 deletions

View File

@ -34,7 +34,7 @@ type rootProp struct {
topLevel bool topLevel bool
} }
func getNodes(s jsonschema.Schema, refs map[string]jsonschema.Schema, a annotationFile) []rootNode { func getNodes(s jsonschema.Schema, refs map[string]jsonschema.Schema, customFields map[string]bool) []rootNode {
rootProps := []rootProp{} rootProps := []rootProp{}
for k, v := range s.Properties { for k, v := range s.Properties {
rootProps = append(rootProps, rootProp{k, v, true}) rootProps = append(rootProps, rootProp{k, v, true})
@ -42,23 +42,24 @@ func getNodes(s jsonschema.Schema, refs map[string]jsonschema.Schema, a annotati
nodes := make([]rootNode, 0, len(rootProps)) nodes := make([]rootNode, 0, len(rootProps))
for i := 0; i < len(rootProps); i++ { for i := 0; i < len(rootProps); i++ {
k := rootProps[i].k item := rootProps[i]
v := rootProps[i].v k := item.k
v := item.v
v = resolveRefs(v, refs) v = resolveRefs(v, refs)
node := rootNode{ node := rootNode{
Title: k, Title: k,
Description: getDescription(v), Description: getDescription(v, item.topLevel),
TopLevel: rootProps[i].topLevel, TopLevel: item.topLevel,
} }
node.Attributes = getAttributes(v.Properties, refs) node.Attributes = getAttributes(v.Properties, refs)
rootProps = append(rootProps, extractNodes(k, v.Properties, refs, a)...) rootProps = append(rootProps, extractNodes(k, v.Properties, refs, customFields)...)
additionalProps, ok := v.AdditionalProperties.(*jsonschema.Schema) additionalProps, ok := v.AdditionalProperties.(*jsonschema.Schema)
if ok { if ok {
objectKeyType := resolveRefs(additionalProps, refs) objectKeyType := resolveRefs(additionalProps, refs)
node.ObjectKeyAttributes = getAttributes(objectKeyType.Properties, refs) node.ObjectKeyAttributes = getAttributes(objectKeyType.Properties, refs)
rootProps = append(rootProps, extractNodes(k, objectKeyType.Properties, refs, a)...) rootProps = append(rootProps, extractNodes(k, objectKeyType.Properties, refs, customFields)...)
} }
if v.Items != nil { if v.Items != nil {
@ -181,7 +182,7 @@ func getAttributes(props map[string]*jsonschema.Schema, refs map[string]jsonsche
attributes = append(attributes, attributeNode{ attributes = append(attributes, attributeNode{
Title: k, Title: k,
Type: typeString, Type: typeString,
Description: getDescription(v), Description: getDescription(v, true),
}) })
} }
sort.Slice(attributes, func(i, j int) bool { sort.Slice(attributes, func(i, j int) bool {
@ -190,8 +191,8 @@ func getAttributes(props map[string]*jsonschema.Schema, refs map[string]jsonsche
return attributes return attributes
} }
func getDescription(s *jsonschema.Schema) string { func getDescription(s *jsonschema.Schema, allowMarkdown bool) string {
if s.MarkdownDescription != "" { if allowMarkdown && s.MarkdownDescription != "" {
return s.MarkdownDescription return s.MarkdownDescription
} }
return s.Description return s.Description
@ -226,14 +227,22 @@ func resolveRefs(s *jsonschema.Schema, schemas map[string]jsonschema.Schema) *js
return node return node
} }
func extractNodes(prefix string, props map[string]*jsonschema.Schema, refs map[string]jsonschema.Schema, a annotationFile) []rootProp { func shouldExtract(ref string, customFields map[string]bool) bool {
refKey := strings.TrimPrefix(ref, "#/$defs/")
_, isCustomField := customFields[refKey]
return isCustomField
}
func extractNodes(prefix string, props map[string]*jsonschema.Schema, refs map[string]jsonschema.Schema, customFields map[string]bool) []rootProp {
nodes := []rootProp{} nodes := []rootProp{}
for k, v := range props { for k, v := range props {
if !shouldExtract(*v.Reference, customFields) {
continue
}
v = resolveRefs(v, refs) v = resolveRefs(v, refs)
if v.Type == "object" { if v.Type == "object" {
nodes = append(nodes, rootProp{prefix + "." + k, v, false}) nodes = append(nodes, rootProp{prefix + "." + k, v, false})
} }
v.MarkdownDescription = ""
} }
return nodes return nodes
} }

File diff suppressed because it is too large Load Diff

View File

@ -91,21 +91,24 @@ type annotation struct {
func generateDocs(workdir, outputPath string) error { func generateDocs(workdir, outputPath string) error {
annotationsPath := filepath.Join(workdir, "annotations.yml") annotationsPath := filepath.Join(workdir, "annotations.yml")
annotationsOpenApiPath := filepath.Join(workdir, "annotations_openapi.yml")
annotationsOpenApiOverridesPath := filepath.Join(workdir, "annotations_openapi_overrides.yml")
annotations, err := LoadAndMergeAnnotations([]string{annotationsPath, annotationsOpenApiPath, annotationsOpenApiOverridesPath}) annotations, err := LoadAndMergeAnnotations([]string{annotationsPath})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
schemas := map[string]jsonschema.Schema{} schemas := map[string]jsonschema.Schema{}
customFields := map[string]bool{}
s, err := jsonschema.FromType(reflect.TypeOf(config.Root{}), []func(reflect.Type, jsonschema.Schema) jsonschema.Schema{ s, err := jsonschema.FromType(reflect.TypeOf(config.Root{}), []func(reflect.Type, jsonschema.Schema) jsonschema.Schema{
removeJobsFields, removeJobsFields,
makeVolumeTypeOptional, makeVolumeTypeOptional,
func(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema { func(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema {
_, isCustomField := annotations[jsonschema.TypePath(typ)]
if isCustomField {
customFields[jsonschema.TypePath(typ)] = true
}
schemas[jsonschema.TypePath(typ)] = s schemas[jsonschema.TypePath(typ)] = s
refPath := getPath(typ) refPath := getPath(typ)
@ -135,7 +138,7 @@ func generateDocs(workdir, outputPath string) error {
log.Fatal(err) log.Fatal(err)
} }
nodes := getNodes(s, schemas, annotations) nodes := getNodes(s, schemas, customFields)
err = buildMarkdown(nodes, outputPath) err = buildMarkdown(nodes, outputPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)