this repo has no description
1// Copyright 2023 CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Package openapi provides OpenAPI encoding and decoding functionality.
16//
17// This is an EXPERIMENTAL API.
18package openapi
19
20import (
21 "cuelang.org/go/cue"
22 "cuelang.org/go/encoding/openapi"
23 "cuelang.org/go/internal/core/adt"
24 "cuelang.org/go/internal/pkg"
25 "cuelang.org/go/internal/value"
26)
27
28var (
29 selfContainedPath = cue.ParsePath("selfContained")
30 expandReferencesPath = cue.ParsePath("expandReferences")
31 infoPath = cue.ParsePath("info")
32)
33
34// Marshal returns the OpenAPI encoding of schema for the given OpenAPI version.
35// The optional config value can be used to make further adjustments.
36//
37// Experimental: this API may change.
38//
39// schema can have the following fields:
40//
41// #Config: {
42// // version holds the OpenAPI version to use when marshaling.
43// // Currently only "3.0.0" is supported.
44// version!: "3.0.0" // currently "3.0.0" only
45
46// // selfContained causes all non-expanded external references
47// // to be included//
48// selfContained?: bool
49//
50// // expandReferences replaces references with actual objects when generating
51// // OpenAPI Schema. It is an error for an CUE value to refer to itself
52// // if this option is used.
53// expandReferences?: bool
54//
55// // info specifies the info section of the OpenAPI document. To be a valid
56// // OpenAPI document, it must include at least the title and version fields.
57// info?: {
58// title: string
59// description: string
60// version: string
61// }
62// }
63func MarshalSchema(config cue.Value, schema pkg.Schema) (string, error) {
64 // TODO: implement a proper struct for schema.
65
66 ctx := value.OpContext(schema)
67 return marshalSchema(ctx, config, schema)
68}
69
70func marshalSchema(_ *adt.OpContext, config cue.Value, schema pkg.Schema) (string, error) {
71 selfContained, _ := config.LookupPath(selfContainedPath).Bool()
72 expandReferences, _ := config.LookupPath(expandReferencesPath).Bool()
73
74 version, err := config.LookupPath(cue.ParsePath("version")).String()
75 if err != nil {
76 return "", err
77 }
78
79 c := &openapi.Config{
80 Version: version,
81 SelfContained: selfContained,
82 ExpandReferences: expandReferences,
83 }
84
85 if info := config.LookupPath(infoPath); info.Exists() {
86 c.Info = info
87 }
88
89 b, err := openapi.Gen(schema, c)
90 if err != nil {
91 return "", err
92 }
93 return string(b), err
94}