Skip to content

Commit

Permalink
morewip
Browse files Browse the repository at this point in the history
  • Loading branch information
manicminer committed Sep 30, 2024
1 parent d0c70d7 commit 5585fc2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func resolveRefsForModel(ref spec.Ref, filePath, topLevelDir string, doc *loads.

if modelFilePath == "" {
// no valid swagger doc was found for this model
return nil
return fmt.Errorf("swagger document not found for ref: %s", ref.String())
}

// decide whether to look for the model definition in the current file, or another referenced file
Expand Down Expand Up @@ -186,7 +186,17 @@ func resolveRefsForModel(ref spec.Ref, filePath, topLevelDir string, doc *loads.
}
}

// if this is a parent model, look for implementations
// look for parent models - this could be a plain child model, or a discriminated implementation
for _, allOf := range resolvedModel.AllOf {
if allOf.Ref.String() != "" {
if err := resolveRefsForModel(allOf.Ref, modelFilePath, topLevelDir, refDoc, refs); err != nil {
return err
}
}
}

// if this is a parent model, look for implementations. this will currently only work for children defined in
// the same swagger file.
if resolvedModel.Discriminator != "" {
for defName, def := range refDoc.Spec().Definitions {
if defName == modelName {
Expand Down Expand Up @@ -216,15 +226,6 @@ func resolveRefsForModel(ref spec.Ref, filePath, topLevelDir string, doc *loads.
}
}

// look for parent models - this could be a plain child model, or a discriminated implementation
for _, allOf := range resolvedModel.AllOf {
if allOf.Ref.String() != "" {
if err := resolveRefsForModel(allOf.Ref, modelFilePath, topLevelDir, refDoc, refs); err != nil {
return err
}
}
}

return nil
}

Expand All @@ -247,6 +248,12 @@ func resolveRefsForProperties(properties spec.SchemaProperties, modelFilePath, t
return err
}
}

if items := prop.AdditionalProperties.Schema.Items; items != nil && items.Schema != nil && items.Schema.Ref.String() != "" {
if err := resolveRefsForModel(items.Schema.Ref, modelFilePath, topLevelDir, refDoc, refs); err != nil {
return err
}
}
}

// look for a model referenced directly by the property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,24 +378,26 @@ func (c *Context) modelDetailsFromObject(modelName string, input spec.Schema, fi
}
}

// this would be an Implementation
if v, ok := input.Extensions.GetString("x-ms-discriminator-value"); ok {
details.DiscriminatedValue = &v

// so we need to find the ancestor details
parentTypeName, discriminator, err := c.FindAncestorType(input)
if err != nil {
return nil, fmt.Errorf("finding ancestor type for %q: %+v", modelName, err)
}
if parentTypeName != nil && discriminator != nil {
details.ParentTypeName = parentTypeName
details.FieldNameContainingDiscriminatedValue = discriminator
// look for a potential ancestor
parentTypeName, discriminator, err := c.FindAncestorType(input)
if err != nil {
return nil, fmt.Errorf("finding ancestor type for %q: %+v", modelName, err)
}
if parentTypeName != nil && discriminator != nil {
details.ParentTypeName = parentTypeName
details.FieldNameContainingDiscriminatedValue = discriminator

// look for the discriminated value, or use the model name
if v, ok := input.Extensions.GetString("x-ms-discriminator-value"); ok {
details.DiscriminatedValue = &v
} else {
details.DiscriminatedValue = pointer.To(modelName)
}
}

// however if there's a Discriminator value defined but no parent type - this is bad data - so we should ignore it
if details.ParentTypeName == nil || details.FieldNameContainingDiscriminatedValue == nil {
details.DiscriminatedValue = nil
}
// if there is a discriminated value but no parent type - this is bad data - so we should ignore it
if details.ParentTypeName == nil || details.FieldNameContainingDiscriminatedValue == nil {
details.DiscriminatedValue = nil
}

return &details, nil
Expand Down

0 comments on commit 5585fc2

Please sign in to comment.