Skip to content

Commit

Permalink
Merge pull request #38 from GetShopTV/fix-unused-#37
Browse files Browse the repository at this point in the history
Stop producing unused definitions
  • Loading branch information
fizruk committed Jan 18, 2016
2 parents ba83080 + 047a3fb commit e0437b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
* Minor changes:
* Minor changes (see [#36](https://github.com/GetShopTV/swagger2/pull/36)):
* Change default `ToSchema` instance for unit data types (i.e. types with one nullable constructor like `data Unit = Unit`):
now these types are treated like sum types with only one alternative;
* Add generic `ToParamSchema` instance for unit data types;
* Add `items: []` to schema for `()` (making it a valid schema).

* Fixes:
* `items: []` is not omitted from `Schema` JSON.
* Do not omit `items: []` from `Schema` JSON;
* Do not generate unused definitions for nested `newtype`s (see [#38](https://github.com/GetShopTV/swagger2/pull/38)).

1.1.1
---
Expand Down
8 changes: 5 additions & 3 deletions src/Data/Swagger/Internal/Schema.hs
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,14 @@ instance {-# OVERLAPPING #-} Constructor c => GToSchema (C1 c U1) where
instance (Selector s, GToSchema f) => GToSchema (C1 c (S1 s f)) where
gdeclareNamedSchema opts _ s
| unwrapUnaryRecords opts = fieldSchema
| otherwise = do
(_, schema) <- recordSchema
| otherwise =
case schema ^. schemaItems of
Just (SwaggerItemsArray [_]) -> fieldSchema
_ -> pure (unnamed schema)
_ -> do
declare defs
return (unnamed schema)
where
(defs, (_, schema)) = runDeclare recordSchema mempty
recordSchema = gdeclareNamedSchema opts (Proxy :: Proxy (S1 s f)) s
fieldSchema = gdeclareNamedSchema opts (Proxy :: Proxy f) s

Expand Down
8 changes: 8 additions & 0 deletions test/Data/Swagger/SchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ spec = do
context "Character" $ checkDefs (Proxy :: Proxy Character) ["Player", "Point"]
context "MyRoseTree" $ checkDefs (Proxy :: Proxy MyRoseTree) ["RoseTree"]
context "[Set (Unit, Maybe Color)]" $ checkDefs (Proxy :: Proxy [Set (Unit, Maybe Color)]) ["Unit", "Color"]
context "ResourceId" $ checkDefs (Proxy :: Proxy ResourceId) []
describe "Inlining Schemas" $ do
context "Paint" $ checkInlinedSchema (Proxy :: Proxy Paint) paintInlinedSchemaJSON
context "Character" $ checkInlinedSchema (Proxy :: Proxy Character) characterInlinedSchemaJSON
Expand Down Expand Up @@ -573,3 +574,10 @@ lightInlinedSchemaJSON = [aesonQQ|
}
|]

-- ========================================================================
-- ResourceId (series of newtypes)
-- ========================================================================

newtype Id = Id String deriving (Generic, ToSchema)

newtype ResourceId = ResourceId Id deriving (Generic, ToSchema)

0 comments on commit e0437b4

Please sign in to comment.