Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible unnecessary reset of schema ref #938

Open
sskserk opened this issue Apr 11, 2024 · 0 comments
Open

Possible unnecessary reset of schema ref #938

sskserk opened this issue Apr 11, 2024 · 0 comments

Comments

@sskserk
Copy link
Contributor

sskserk commented Apr 11, 2024

Hi @fenollp,

It seems that cleaning the ref.Value https://github.com/getkin/kin-openapi/blob/v0.124.0/openapi3gen/openapi3gen.go#L150 is no more actual. A type refers a subtype, thus it is necessary not only to preserve the Ref but also the Value. Otherwise the validation of schema fails.

Don't you think so?

CC @EnriqueL8

func (g *Generator) NewSchemaRefForValue(value interface{}, schemas openapi3.Schemas) (*openapi3.SchemaRef, error) {
	ref, err := g.GenerateSchemaRef(reflect.TypeOf(value))
	if err != nil {
		return nil, err
	}
	for ref := range g.SchemaRefs {
		refName := ref.Ref
		if g.opts.exportComponentSchemas.ExportComponentSchemas && strings.HasPrefix(refName, "#/components/schemas/") {
			refName = strings.TrimPrefix(refName, "#/components/schemas/")
		}

		if _, ok := g.componentSchemaRefs[refName]; ok && schemas != nil {
			if ref.Value != nil && ref.Value.Properties != nil {
				schemas[refName] = &openapi3.SchemaRef{
					Value: ref.Value,
				}
			}
		}
		if strings.HasPrefix(ref.Ref, "#/components/schemas/") {
			ref.Value = nil
		} else {
			ref.Ref = ""
		}
	}
	return ref, nil
}
@sskserk sskserk changed the title Possible reset of schema ref Possible unnecessary reset of schema ref Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant