Skip to content

Commit

Permalink
Merge pull request #413 from tdakkota/feat/parse-more-fields
Browse files Browse the repository at this point in the history
feat: parse more spec fields
  • Loading branch information
ernado authored Jun 22, 2022
2 parents 34ee8b0 + c3c2aaa commit c2a868c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
8 changes: 4 additions & 4 deletions dsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func TestBuilder(t *testing.T) {
Version: "0.1.0",
},
Servers: []ogen.Server{
{"staging", "staging.api.com"},
{"production", "api.com"},
{"staging", "staging.api.com", nil},
{"production", "api.com", nil},
},
Paths: map[string]*ogen.PathItem{
pathWithID: {
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestBuilder(t *testing.T) {
SetHead(ogen.NewOperation().SetOperationID("head").SetResponses(ogen.Responses{"resp": ogen.NewResponse()})).
SetPatch(ogen.NewOperation().SetOperationID("patch").AddParameters(ogen.NewParameter().InHeader().SetDeprecated(true))).
SetTrace(ogen.NewOperation().SetOperationID("trace")).
SetServers([]ogen.Server{{"desc1", "url1"}}).
SetServers([]ogen.Server{{"url1", "desc1", nil}}).
AddServers(ogen.NewServer().SetDescription("desc2").SetURL("url2")).
SetParameters([]*ogen.Parameter{_queryParam.Parameter})
assert.Equal(t, &ogen.PathItem{
Expand All @@ -337,7 +337,7 @@ func TestBuilder(t *testing.T) {
Head: &ogen.Operation{OperationID: "head", Responses: ogen.Responses{"resp": &ogen.Response{}}},
Patch: &ogen.Operation{OperationID: "patch", Parameters: []*ogen.Parameter{{In: "header", Deprecated: true}}},
Trace: &ogen.Operation{OperationID: "trace"},
Servers: []ogen.Server{{"desc1", "url1"}, {"desc2", "url2"}},
Servers: []ogen.Server{{"url1", "desc1", nil}, {"url2", "desc2", nil}},
Parameters: []*ogen.Parameter{_queryParam.Parameter},
}, pi)

Expand Down
2 changes: 2 additions & 0 deletions jsonschema/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ func (p *Parser) parseMany(schemas []*RawSchema, ctx *resolveCtx) ([]*Schema, er
}

func (p *Parser) extendInfo(schema *RawSchema, s *Schema) *Schema {
s.ContentEncoding = schema.ContentEncoding
s.ContentMediaType = schema.ContentMediaType
s.Summary = schema.Summary
s.Description = schema.Description
s.Deprecated = schema.Deprecated
Expand Down
2 changes: 2 additions & 0 deletions jsonschema/raw_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type RawSchema struct {
Default json.RawMessage `json:"default,omitempty"`
Example json.RawMessage `json:"example,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
ContentEncoding string `json:"contentEncoding,omitempty"`
ContentMediaType string `json:"contentMediaType,omitempty"`
XAnnotations map[string]jx.Raw `json:"-"`
}

Expand Down
10 changes: 7 additions & 3 deletions jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ const (
type Schema struct {
XOgenName string // Annotation to set type name.

Type SchemaType
Format string // Schema format, optional.
Ref string // Whether schema is referenced.

Type SchemaType
Format string // Schema format, optional.
ContentEncoding string
ContentMediaType string

Summary string // Schema summary from Reference Object, optional.
Description string // Schema description, optional.
Deprecated bool
Ref string // Whether schema is referenced.

Item *Schema // Only for Array and Object with additional properties.
AdditionalProperties *bool // Whether Object has additional properties.
Expand Down
69 changes: 64 additions & 5 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Spec struct {
// Each tag name in the list MUST be unique.
Tags []Tag `json:"tags,omitempty"`

// Additional external documentation.
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`

// Raw JSON value. Used by JSON Schema resolver.
Raw []byte `json:"-"`
}
Expand Down Expand Up @@ -91,8 +94,9 @@ type Example struct {
//
// https://swagger.io/specification/#tag-object
type Tag struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
}

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

// Server represents a Server.
type Server struct {
// REQUIRED. A URL to the target host. This URL supports Server Variables and MAY be relative,
// to indicate that the host location is relative to the location where the OpenAPI document is being served.
// Variable substitutions will be made when a variable is named in {brackets}.
URL string `json:"url"`
// An optional string describing the host designated by the URL.
// CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty"`
// A map between a variable name and its value. The value is used for substitution in the server's URL template.
Variables map[string]ServerVariable `json:"variables,omitempty"`
}

// ServerVariable describes an object representing a Server Variable for server URL template substitution.
type ServerVariable struct {
// An enumeration of string values to be used if the substitution options are from a limited set.
//
// The array MUST NOT be empty.
Enum []string `json:"enum,omitempty"`
// REQUIRED. The default value to use for substitution, which SHALL be sent if an alternate value is not supplied.
// Note this behavior is different than the Schema Object’s treatment of default values, because in those
// cases parameter values are optional. If the enum is defined, the value MUST exist in the enum’s values.
Default string `json:"default"`
// An optional description for the server variable. CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty"`
URL string `json:"url"`
}

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

Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`

OperationID string `json:"operationId,omitempty"`
Parameters []*Parameter `json:"parameters,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty"`
Expand All @@ -196,6 +223,14 @@ type Operation struct {
Deprecated bool `json:"deprecated,omitempty"`
}

// ExternalDocumentation describes a reference to external resource for extended documentation.
type ExternalDocumentation struct {
// A description of the target documentation. CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty"`
// REQUIRED. The URL for the target documentation. This MUST be in the form of a URL.
URL string `json:"url"`
}

// Parameter describes a single operation parameter.
// A unique parameter is defined by a combination of a name and location.
type Parameter struct {
Expand Down Expand Up @@ -333,6 +368,9 @@ type Schema struct {
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`

// Additional external documentation for this schema.
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`

// Value MUST be a string. Multiple types via an array are not supported.
Type string `json:"type,omitempty"`

Expand Down Expand Up @@ -526,6 +564,27 @@ type Schema struct {
// Specifies that a schema is deprecated and SHOULD be transitioned out
// of usage.
Deprecated bool `json:"deprecated,omitempty"`

// If the instance value is a string, this property defines that the
// string SHOULD be interpreted as binary data and decoded using the
// encoding named by this property. RFC 2045, Section 6.1 lists
// the possible values for this property.
//
// The value of this property MUST be a string.
//
// The value of this property SHOULD be ignored if the instance
// described is not a string.
ContentEncoding string `json:"contentEncoding,omitempty"`

// The value of this property must be a media type, as defined by RFC
// 2046. This property defines the media type of instances
// which this schema defines.
//
// The value of this property MUST be a string.
//
// The value of this property SHOULD be ignored if the instance
// described is not a string.
ContentMediaType string `json:"contentMediaType,omitempty"`
}

// Property is item of Properties.
Expand Down
9 changes: 8 additions & 1 deletion spec_backcomp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package ogen

import "github.com/ogen-go/ogen/jsonschema"
import (
"github.com/go-faster/jx"

"github.com/ogen-go/ogen/jsonschema"
)

// ToJSONSchema converts Schema to jsonschema.Schema.
func (s *Schema) ToJSONSchema() *jsonschema.RawSchema {
Expand Down Expand Up @@ -49,6 +53,9 @@ func (s *Schema) ToJSONSchema() *jsonschema.RawSchema {
Default: s.Default,
Example: s.Example,
Deprecated: s.Deprecated,
ContentEncoding: s.ContentEncoding,
ContentMediaType: s.ContentMediaType,
XAnnotations: map[string]jx.Raw{},
}
}

Expand Down

0 comments on commit c2a868c

Please sign in to comment.