Skip to content

Generated swagger may have undefined references #224

@kaol

Description

@kaol

It's possible for a Swagger spec to have undefined references. The following check returns a nonempty list if that happens. Any such swagger spec will cause trouble with any tools trying to use it. I'm not quite sure where it would fit in Data.Swagger.Schema.Validation as those concern validating data against a spec but this is about the validity of the spec itself.

Here's an example of a faulty Swagger file. I'm afraid it's not a minimal example. Running openapi-spec-validator --schema 2.0 on it gives an error Unresolvable JSON pointer: 'definitions/PaperCode'. I didn't see that swagger2 library had any call that would have given the same information about it.

import Control.Lens
import qualified Data.Set as Set
import Data.Swagger
import qualified Data.HashMap.Strict.InsOrd as InsOrd

missingRefs :: Swagger -> Bool
missingRegs swagger = Set.null $ Set.difference usedRefs swaggerDefinitions
  where
    usedRefs = Set.fromList . catMaybes . map getRef . concatMap (toList . view properties) . toList $ swagger ^. definitions
    getRef (Inline _) = Nothing
    getRef (Ref r) = Just $ getReference r
    swaggerDefinitions = Set.fromList . map fst . InsOrd.toList $ swagger ^. definitions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions