The first expression is almost correct; it is correct for $y=0$ but not as we go further from equilibrium.
Let's ignore the piston for the rest of this answer, you know the $P~dV$ stuff, you're fine with that. The spring is confusing you. I will even take a step back as a sort of "refresher" on springs.
We know that we have some spring with some spring constant $k$ and some equilibrium length $y_0$ and we place some mass $m$ upon it. We know that it compresses a little bit, to satisfy $$m \frac{d^2y}{dt^2} = -k (y -y_0) - m g.$$So you might think "okay, this is already at some $y\ne y_0$ when I do all of this other stuff with it, I probably have to add that into my potential energy." However, and this may surprise you if you haven't seen it before, we can just incorporate the $mg$ constant into the $y_0$ to find $y_0' = y_0 - mg/k,$ so that the force is just $-k(y - y_0')$ with no explicit $mg$ term. Then a suitable potential energy is $\frac12 k (y - y_0')^2.$
Control question: prove that this is also the potential energy you get, up to a constant, when you use the spring's $y_0$ potential energy explicitly but include $mgy$ for the potential energy of gravity.
So now you say, "okay, set $y_0' = 0$ by a serendipitous choice of coordinates!" and that is valid, now the potential energy is $\frac 12 k y^2.$ That is fine, too.
So now you say "okay, the work needed to compress this thing by an amount $\Delta y$ is $\frac12 k (\Delta y)^2,$" and that is correct, but only for compressing it an arbitrary amount around its equilibrium at $y=0$. It would not be adequate for what you're suggesting, which is integrating over $dy$ to find total work, because that is fundamentally a Riemann sum:
We take a step of size $\delta y$ from $y=0$, find a work $\delta W_1,$ then we take a step of size $\delta y$ from $y = \delta y$, find a work $\delta W_2,$ [...] then we take a step of size $\delta y$ from $y = (n-1)\delta y,$ find a work $\delta W_n$, now the work done as $y$ varies from $0$ to $n~\delta y$, is approximately $\delta W_1 + \dots + \delta W_n,$ with the approximation getting better as $n$ goes to infinity and $\delta y$ commensurately goes to 0.
Notice that these must both agree at the end but the $dy^2$ approach is not the right way to get the above process going, because it only is correct for calculating $\delta W_1$ and then its assumption that you're starting from $y=0$ is invalid for all of the other terms.
Instead for the above process you will want to investigate, $$dU = U(y + dy) - U(y) = \frac12 k (y + dy)^2 - \frac 12 k y^2 = k~y~dy + \frac 12 ~k~dy^2.$$For small enough steps $dy$ you can neglect the second term and you simply have the first term which... (fanfare)... is precisely the work done against that spring force $F = -k y$.