It is not obvious such contracts are legal without any form of compensation. E.g. some company have a clause where if you invent something in your free time they can choose to buy the invention from you at a fixed price determined by the contract, and that is legal. But it is not as clear that just getting all intellectual property without any extra compensation is legal. A contract need to be quid pro quo, and in addition to that local employment law can contain limitations.