Beispiel #1
0
// Expanded expands the ref fields in the spec document and returns a new spec document
func (d *Document) Expanded(options ...*spec.ExpandOptions) (*Document, error) {
	swspec := new(spec.Swagger)
	if err := json.Unmarshal(d.raw, swspec); err != nil {
		return nil, err
	}

	var expandOptions *spec.ExpandOptions
	if len(options) > 0 {
		expandOptions = options[1]
	} else {
		expandOptions = &spec.ExpandOptions{
			RelativeBase: filepath.Dir(d.specFilePath),
		}
	}

	if err := spec.ExpandSpec(swspec, expandOptions); err != nil {
		return nil, err
	}

	dd := &Document{
		Analyzer: analysis.New(swspec),
		spec:     swspec,
		schema:   spec.MustLoadSwagger20Schema(),
		raw:      d.raw,
		origSpec: d.origSpec,
	}
	return dd, nil
}
Beispiel #2
0
// Expanded expands the ref fields in the spec document and returns a new spec document
func (d *Document) Expanded() (*Document, error) {
	swspec := new(spec.Swagger)
	if err := json.Unmarshal(d.raw, swspec); err != nil {
		return nil, err
	}
	if err := spec.ExpandSpec(swspec); err != nil {
		return nil, err
	}

	dd := &Document{
		Analyzer: analysis.New(swspec),
		spec:     swspec,
		schema:   swag20Schema,
		raw:      d.raw,
		origSpec: d.origSpec,
	}
	return dd, nil
}
Beispiel #3
0
// Flatten an analyzed spec.
//
// To flatten a spec means:
//
// Expand the parameters, responses, path items, parameter items and header items.
// Import external (http, file) references so they become internal to the document.
// Move every inline schema to be a definition with an auto-generated name in a depth-first fashion.
// Rewritten schemas get a vendor extension x-go-gen-location so we know in which package they need to be rendered.
func Flatten(opts FlattenOpts) error {
	// recursively expand responses, parameters, path items and items
	err := swspec.ExpandSpec(opts.Swagger(), opts.ExpandOpts(true))
	if err != nil {
		return err
	}
	opts.Spec.reload() // re-analyze

	// at this point there are no other references left but schemas
	if err := importExternalReferences(&opts); err != nil {
		return err
	}
	opts.Spec.reload() // re-analyze

	// rewrite the inline schemas (schemas that aren't simple types or arrays of simple types)
	if err := nameInlinedSchemas(&opts); err != nil {
		return err
	}
	opts.Spec.reload() // re-analyze

	// TODO: simplifiy known schema patterns to flat objects with properties?
	return nil
}