GetShopTV / swagger2

Swagger 2.0 data model.
http://hackage.haskell.org/package/swagger2
BSD 3-Clause "New" or "Revised" License
74 stars 59 forks source link

Generated swagger may have undefined references #224

Open kaol opened 3 years ago

kaol commented 3 years ago

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