Example #1
0
func (a *AnalyzedSchema) inferFromRef() error {
	if a.hasRef {
		opts := &spec.ExpandOptions{RelativeBase: a.basePath}
		sch, err := spec.ResolveRefWithBase(a.root, &a.schema.Ref, opts)
		if err != nil {
			return err
		}
		if sch != nil {
			rsch, err := Schema(SchemaOpts{
				Schema:   sch,
				Root:     a.root,
				BasePath: a.basePath,
			})
			if err != nil {
				return err
			}
			a.inherits(rsch)
		}
	}
	return nil
}
Example #2
0
func importExternalReferences(opts *FlattenOpts) error {
	groupedRefs := reverseIndexForSchemaRefs(opts)

	for refStr, entry := range groupedRefs {
		if !entry.Ref.HasFragmentOnly {
			if swspec.Debug {
				log.Printf("importing external schema for [%s] from %s", strings.Join(entry.Keys, ", "), refStr)
			}
			// resolve to actual schema
			sch, err := swspec.ResolveRefWithBase(opts.Swagger(), &entry.Ref, opts.ExpandOpts(false))
			if err != nil {
				return err
			}
			if sch == nil {
				return fmt.Errorf("no schema found at %s for [%s]", refStr, strings.Join(entry.Keys, ", "))
			}
			if swspec.Debug {
				log.Printf("importing external schema for [%s] from %s", strings.Join(entry.Keys, ", "), refStr)
			}

			// generate a unique name
			newName := uniqifyName(opts.Swagger().Definitions, nameFromRef(entry.Ref))
			if swspec.Debug {
				log.Printf("new name for [%s]: %s", strings.Join(entry.Keys, ", "), newName)
			}

			// rewrite the external refs to local ones
			for _, key := range entry.Keys {
				if err := updateRef(opts.Swagger(), key, swspec.MustCreateRef("#"+path.Join("/definitions", newName))); err != nil {
					return err
				}
			}

			// add the resolved schema to the definitions
			saveSchema(opts.Swagger(), newName, sch)
		}
	}
	return nil
}
Example #3
0
func (s *SpecValidator) resolveRef(ref *spec.Ref) (*spec.Schema, error) {
	if s.spec.SpecFilePath() != "" {
		return spec.ResolveRefWithBase(s.spec.Spec(), ref, &spec.ExpandOptions{RelativeBase: s.spec.SpecFilePath()})
	}
	return spec.ResolveRef(s.spec.Spec(), ref)
}