Skip to content

Commit c2a868c

Browse files
authored
Merge pull request #413 from tdakkota/feat/parse-more-fields
feat: parse more spec fields
2 parents 34ee8b0 + c3c2aaa commit c2a868c

File tree

6 files changed

+87
-13
lines changed

6 files changed

+87
-13
lines changed

dsl_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ func TestBuilder(t *testing.T) {
102102
Version: "0.1.0",
103103
},
104104
Servers: []ogen.Server{
105-
{"staging", "staging.api.com"},
106-
{"production", "api.com"},
105+
{"staging", "staging.api.com", nil},
106+
{"production", "api.com", nil},
107107
},
108108
Paths: map[string]*ogen.PathItem{
109109
pathWithID: {
@@ -327,7 +327,7 @@ func TestBuilder(t *testing.T) {
327327
SetHead(ogen.NewOperation().SetOperationID("head").SetResponses(ogen.Responses{"resp": ogen.NewResponse()})).
328328
SetPatch(ogen.NewOperation().SetOperationID("patch").AddParameters(ogen.NewParameter().InHeader().SetDeprecated(true))).
329329
SetTrace(ogen.NewOperation().SetOperationID("trace")).
330-
SetServers([]ogen.Server{{"desc1", "url1"}}).
330+
SetServers([]ogen.Server{{"url1", "desc1", nil}}).
331331
AddServers(ogen.NewServer().SetDescription("desc2").SetURL("url2")).
332332
SetParameters([]*ogen.Parameter{_queryParam.Parameter})
333333
assert.Equal(t, &ogen.PathItem{
@@ -337,7 +337,7 @@ func TestBuilder(t *testing.T) {
337337
Head: &ogen.Operation{OperationID: "head", Responses: ogen.Responses{"resp": &ogen.Response{}}},
338338
Patch: &ogen.Operation{OperationID: "patch", Parameters: []*ogen.Parameter{{In: "header", Deprecated: true}}},
339339
Trace: &ogen.Operation{OperationID: "trace"},
340-
Servers: []ogen.Server{{"desc1", "url1"}, {"desc2", "url2"}},
340+
Servers: []ogen.Server{{"url1", "desc1", nil}, {"url2", "desc2", nil}},
341341
Parameters: []*ogen.Parameter{_queryParam.Parameter},
342342
}, pi)
343343

jsonschema/parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ func (p *Parser) parseMany(schemas []*RawSchema, ctx *resolveCtx) ([]*Schema, er
367367
}
368368

369369
func (p *Parser) extendInfo(schema *RawSchema, s *Schema) *Schema {
370+
s.ContentEncoding = schema.ContentEncoding
371+
s.ContentMediaType = schema.ContentMediaType
370372
s.Summary = schema.Summary
371373
s.Description = schema.Description
372374
s.Deprecated = schema.Deprecated

jsonschema/raw_schema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type RawSchema struct {
6666
Default json.RawMessage `json:"default,omitempty"`
6767
Example json.RawMessage `json:"example,omitempty"`
6868
Deprecated bool `json:"deprecated,omitempty"`
69+
ContentEncoding string `json:"contentEncoding,omitempty"`
70+
ContentMediaType string `json:"contentMediaType,omitempty"`
6971
XAnnotations map[string]jx.Raw `json:"-"`
7072
}
7173

jsonschema/schema.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ const (
3131
type Schema struct {
3232
XOgenName string // Annotation to set type name.
3333

34-
Type SchemaType
35-
Format string // Schema format, optional.
34+
Ref string // Whether schema is referenced.
35+
36+
Type SchemaType
37+
Format string // Schema format, optional.
38+
ContentEncoding string
39+
ContentMediaType string
40+
3641
Summary string // Schema summary from Reference Object, optional.
3742
Description string // Schema description, optional.
3843
Deprecated bool
39-
Ref string // Whether schema is referenced.
4044

4145
Item *Schema // Only for Array and Object with additional properties.
4246
AdditionalProperties *bool // Whether Object has additional properties.

spec.go

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type Spec struct {
3434
// Each tag name in the list MUST be unique.
3535
Tags []Tag `json:"tags,omitempty"`
3636

37+
// Additional external documentation.
38+
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
39+
3740
// Raw JSON value. Used by JSON Schema resolver.
3841
Raw []byte `json:"-"`
3942
}
@@ -91,8 +94,9 @@ type Example struct {
9194
//
9295
// https://swagger.io/specification/#tag-object
9396
type Tag struct {
94-
Name string `json:"name"`
95-
Description string `json:"description,omitempty"`
97+
Name string `json:"name"`
98+
Description string `json:"description,omitempty"`
99+
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
96100
}
97101

98102
// Info provides metadata about the API.
@@ -132,8 +136,29 @@ type License struct {
132136

133137
// Server represents a Server.
134138
type Server struct {
139+
// REQUIRED. A URL to the target host. This URL supports Server Variables and MAY be relative,
140+
// to indicate that the host location is relative to the location where the OpenAPI document is being served.
141+
// Variable substitutions will be made when a variable is named in {brackets}.
142+
URL string `json:"url"`
143+
// An optional string describing the host designated by the URL.
144+
// CommonMark syntax MAY be used for rich text representation.
145+
Description string `json:"description,omitempty"`
146+
// A map between a variable name and its value. The value is used for substitution in the server's URL template.
147+
Variables map[string]ServerVariable `json:"variables,omitempty"`
148+
}
149+
150+
// ServerVariable describes an object representing a Server Variable for server URL template substitution.
151+
type ServerVariable struct {
152+
// An enumeration of string values to be used if the substitution options are from a limited set.
153+
//
154+
// The array MUST NOT be empty.
155+
Enum []string `json:"enum,omitempty"`
156+
// REQUIRED. The default value to use for substitution, which SHALL be sent if an alternate value is not supplied.
157+
// Note this behavior is different than the Schema Object’s treatment of default values, because in those
158+
// cases parameter values are optional. If the enum is defined, the value MUST exist in the enum’s values.
159+
Default string `json:"default"`
160+
// An optional description for the server variable. CommonMark syntax MAY be used for rich text representation.
135161
Description string `json:"description,omitempty"`
136-
URL string `json:"url"`
137162
}
138163

139164
// Components hold a set of reusable objects for different aspects of the OAS.
@@ -186,8 +211,10 @@ type Operation struct {
186211
// Tags can be used for logical grouping of operations by resources or any other qualifier.
187212
Tags []string `json:"tags,omitempty"`
188213

189-
Summary string `json:"summary,omitempty"`
190-
Description string `json:"description,omitempty"`
214+
Summary string `json:"summary,omitempty"`
215+
Description string `json:"description,omitempty"`
216+
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
217+
191218
OperationID string `json:"operationId,omitempty"`
192219
Parameters []*Parameter `json:"parameters,omitempty"`
193220
RequestBody *RequestBody `json:"requestBody,omitempty"`
@@ -196,6 +223,14 @@ type Operation struct {
196223
Deprecated bool `json:"deprecated,omitempty"`
197224
}
198225

226+
// ExternalDocumentation describes a reference to external resource for extended documentation.
227+
type ExternalDocumentation struct {
228+
// A description of the target documentation. CommonMark syntax MAY be used for rich text representation.
229+
Description string `json:"description,omitempty"`
230+
// REQUIRED. The URL for the target documentation. This MUST be in the form of a URL.
231+
URL string `json:"url"`
232+
}
233+
199234
// Parameter describes a single operation parameter.
200235
// A unique parameter is defined by a combination of a name and location.
201236
type Parameter struct {
@@ -333,6 +368,9 @@ type Schema struct {
333368
Summary string `json:"summary,omitempty"`
334369
Description string `json:"description,omitempty"`
335370

371+
// Additional external documentation for this schema.
372+
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
373+
336374
// Value MUST be a string. Multiple types via an array are not supported.
337375
Type string `json:"type,omitempty"`
338376

@@ -526,6 +564,27 @@ type Schema struct {
526564
// Specifies that a schema is deprecated and SHOULD be transitioned out
527565
// of usage.
528566
Deprecated bool `json:"deprecated,omitempty"`
567+
568+
// If the instance value is a string, this property defines that the
569+
// string SHOULD be interpreted as binary data and decoded using the
570+
// encoding named by this property. RFC 2045, Section 6.1 lists
571+
// the possible values for this property.
572+
//
573+
// The value of this property MUST be a string.
574+
//
575+
// The value of this property SHOULD be ignored if the instance
576+
// described is not a string.
577+
ContentEncoding string `json:"contentEncoding,omitempty"`
578+
579+
// The value of this property must be a media type, as defined by RFC
580+
// 2046. This property defines the media type of instances
581+
// which this schema defines.
582+
//
583+
// The value of this property MUST be a string.
584+
//
585+
// The value of this property SHOULD be ignored if the instance
586+
// described is not a string.
587+
ContentMediaType string `json:"contentMediaType,omitempty"`
529588
}
530589

531590
// Property is item of Properties.

spec_backcomp.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package ogen
22

3-
import "github.com/ogen-go/ogen/jsonschema"
3+
import (
4+
"github.com/go-faster/jx"
5+
6+
"github.com/ogen-go/ogen/jsonschema"
7+
)
48

59
// ToJSONSchema converts Schema to jsonschema.Schema.
610
func (s *Schema) ToJSONSchema() *jsonschema.RawSchema {
@@ -49,6 +53,9 @@ func (s *Schema) ToJSONSchema() *jsonschema.RawSchema {
4953
Default: s.Default,
5054
Example: s.Example,
5155
Deprecated: s.Deprecated,
56+
ContentEncoding: s.ContentEncoding,
57+
ContentMediaType: s.ContentMediaType,
58+
XAnnotations: map[string]jx.Raw{},
5259
}
5360
}
5461

0 commit comments

Comments
 (0)