From 5585fc2d2f8d24133092382c7ca2829f9b8b2d3e Mon Sep 17 00:00:00 2001 From: Tom Bamford Date: Mon, 30 Sep 2024 22:49:49 +0100 Subject: [PATCH] morewip --- .../parser/parsingcontext/build.go | 29 ++++++++++------ .../parser/parsingcontext/parse_model.go | 34 ++++++++++--------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/build.go b/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/build.go index 441da5b55af..abf06d7c718 100644 --- a/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/build.go +++ b/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/build.go @@ -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 @@ -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 { @@ -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 } @@ -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 diff --git a/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/parse_model.go b/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/parse_model.go index cbac3186404..93f1c7892ec 100644 --- a/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/parse_model.go +++ b/tools/importer-rest-api-specs/internal/components/apidefinitions/parser/parsingcontext/parse_model.go @@ -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